VB使用ADO数据库可以分为有源数据库和无源数据库,即是否使用了DSN数据源,如下例:
1、在连接数据库前首先要在VB菜单中“工程”-“引用”从中选择 microsoft activeX Data objects 2.6 library和 microsoft activeX Data objects recordset 2.6两个组件,这是在连接数据前所必做的工作。
2、接下来定义ADO的连接对象,如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
1、在连接数据库前首先要在VB菜单中“工程”-“引用”从中选择 microsoft activeX Data objects 2.6 library和 microsoft activeX Data objects recordset 2.6两个组件,这是在连接数据前所必做的工作。
2、接下来定义ADO的连接对象,如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
第一种方法采用的是无源的数据库连接方法
conn.ConnectionString = "Driver={sql server};server=JSZX3-11;uid=sa;pwd=;database=wzc"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名,密码 from login where 用户名='" & Trim(Combo1.Text) & "' And 密码='" & Trim(Text1.Text) & "'",conn,adOpenStatic,adLockReadOnly,adCmdText
If rs.EOF = True And rs.BOF Then
m = MsgBox("请重新登录",vbExclamation)
Text1.Text = ""
Text1.SetFocus
Else
Form1.Hide
End If
rs.Close
conn.ConnectionString = "Driver={sql server};server=JSZX3-11;uid=sa;pwd=;database=wzc"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名,密码 from login where 用户名='" & Trim(Combo1.Text) & "' And 密码='" & Trim(Text1.Text) & "'",conn,adOpenStatic,adLockReadOnly,adCmdText
If rs.EOF = True And rs.BOF Then
m = MsgBox("请重新登录",vbExclamation)
Text1.Text = ""
Text1.SetFocus
Else
Form1.Hide
End If
rs.Close
这是第二种方法连接数据库的方法,这一种方法是采用有源的方法与数据库连接的方法,代码如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "DSN=login;uid=sa;pwd=;"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名 from login",adCmdText
Dim i As String
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("用户名").Value)
rs.MoveNext
Combo1.AddItem i
Next t
rs.Close
申明:
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDc As Long,ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long,ByVal hDc As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long,ByVal hDc As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
'按钮的代码:
Private Sub 命令1_Click() Dim dc As Long dc = GetDC(0) If GetDeviceCaps(dc,8) = 1024 Then DoCmd.OpenForm "窗体1" ElseIf GetDeviceCaps(dc,8) = 800 Then DoCmd.OpenForm "窗体2" End If End Sub
原文链接:https://www.f2er.com/vb/262461.html