Sub Click(Source As Button)
Dim ws as new notesuiworkspace
dim ss as new notesSession
dim db as notesdatabase
dim doc as notesdocument
set odbcCon = New odbcconnection
set db = ss.currentdatabase
set doc = db.Createdocument
doc.availablesources=odbccon.listdatasources
Call ws.DialogBox("odbc",true,true,false,false,false,false,"选择 数据源",doc)
sourceName$=doc.datasource(0)
if sourceName$= "" then
messagebox "您没有指定数据源名称"
exit sub
end if
if not odbccon.connectto(sourceName$) then
if messagebox("连接失败,是否需要通过口令再登录一次?",4,"") then
username$=Inputbox$("输入用户名:")
passwd$=Inputbox$("输入口令:")
if not odbccon.connectto(sourceName$,username$,passwd$) then
messagebox "连接失败"
exit sub
end if
end if
exit sub
end if
messagebox "已成功连接到数据源"
end sub
Sub Click(Source As Button)
Dim ws As New notesuiworkspace
Dim uidoc As notesuidocument
Dim doc As notesdocument
Dim con As New ODBCConnection
Dim qry As New ODBCQuery
Dim result As New ODBCResultSet
Set qry.Connection = con
Set result.Query = qry
Set uidoc=ws.currentdocument
Set doc=uidoc.document
If (doc.gh(0)="") Or (doc.xm(0)="") Then
Messagebox"工号与姓名是必填内容!"
Exit Sub
End If
If con.connectto("test","fan","fan") Then
'检察是修改还是录入
If doc.option(0)="修改" Then
qry.sql="delete fan where 工号='"&doc.gh(0)&"'"
result.execute
qry.sql="insert into fan values ('"&doc.gh(0)&"','"&doc.bmh(0)&"','"&doc.xm(0)&"','"&doc.gwgz(0)&"','"&doc.jlgz(0)&"','"&doc.zhbt(0)&"','"&doc.qtbt(0)&"','"&doc.bf(0)&"','"&doc.yfgz(0)&"','"&doc.gjj(0)&"','"&doc.ylbx(0)&"','"&doc.sybx(0)&"','"&doc.ff(0)&"','"&doc.grsds(0)&"','"&doc.qt(0)&"','"&doc.xl(0)&"','"&doc.sfgz(0)&"','"&doc.yf(0)&"','"&doc.zhmc(0)&"')"
result.execute
Messagebox"修改完毕!"
Exit Sub
End If
'检察是否有重复
qry.sql="select count(*) as aa from fan where 工号='"&doc.gh(0)&"'"
result.execute
If Cint(result.getvalue("aa"))>0 Then
Messagebox"这个工号有人使用,请重新写!"
Exit Sub
End If
qry.sql="insert into fan values ('"&doc.gh(0)&"','"&doc.bmh(0)&"','"&doc.xm(0)&"','"&doc.gwgz(0)&"','"&doc.jlgz(0)&"','"&doc.zhbt(0)&"','"&doc.qtbt(0)&"','"&doc.bf(0)&"','"&doc.yfgz(0)&"','"&doc.gjj(0)&"','"&doc.ylbx(0)&"','"&doc.sybx(0)&"','"&doc.ff(0)&"','"&doc.grsds(0)&"','"&doc.qt(0)&"','"&doc.xl(0)&"','"&doc.sfgz(0)&"','"&doc.yf(0)&"','"&doc.zhmc(0)&"')"
result.execute
con.disconnect
End If
Messagebox"保存完毕"
Call uidoc.close()
End Sub
Uselsx "*LSXODBC"
Sub Initialize
Dim con As New ODBCConnection
Dim qry As New ODBCQuery
Dim result As New ODBCResultSet
Dim msg As String
Set qry.Connection = con
Set result.Query = qry
con.ConnectTo("ATDB")
qry.SQL = "SELECT * FROM STUDENTS ORDER BY LASTNAME"
result.Execute
result.LastRow ' Fetch all data into memory at start
soughtrow = Inputbox _
("Which row do you want to change?", "Which row?")
If soughtrow = "" Then
Messagebox "No value entered for row",, "Bad row"
Exit Sub
End If
If soughtrow < 1 Or soughtrow > result.NumRows Then
Messagebox "Row out of range",, "Bad row"
Exit Sub
End If
result.CurrentRow = soughtrow
For i = 1 To result.NumColumns
msg = msg & " " & result.GetValue(i)
Next
soughtcolumn = Inputbox(msg, "Which column?")
If soughtcolumn = "" Then
Messagebox "No value entered for column",, _
"Bad column"
Exit Sub
End If
If soughtcolumn = "" Or soughtcolumn < 1 _
Or soughtcolumn > result.NumColumns Then
Messagebox "Column out of range",, "Bad column"
Exit Sub
End If
newValue = Inputbox$("Enter new value", "SetValue", _
result.GetValue(Cint(soughtcolumn)))
If newValue = "" Then
Messagebox "No value entered"
Exit Sub
End If
Call result.SetValue(Cint(soughtcolumn), newValue)
result.UpdateRow
result.Close(DB_CLOSE)
con.Disconnect
End Sub
连接到odbc首先的得建一个odbc数据源,剩下的就是编程工作了。
下面是一个连接odbc的例子
Uselsx "*LSXODBC"
'这句是必需的,否则无法ocbc的方法
Sub Initialize
Dim con As New ODBCConnection
Dim dsn As String
Dim msg As String
dsn = Inputbox("ODBC data source name", "DSN")
REM If Not con.ConnectTo(dsn) Then
con.ConnectTo(dsn)
If Not con.IsConnected Then
Messagebox "Could not connect to " & dsn,, _
"Error"
Exit Sub
End If
tables = con.ListTables(dsn)
msg = dsn & " contains the following _
tables:" & Chr(10)
For n% = Lbound(tables) To Ubound(tables)
msg = msg & Chr(10) & tables(n%)
Next
Messagebox msg,, "Tables for " & dsn
con.Disconnect
End Sub