|
先使用基于mdb的数据库表创建绑定窗体,然后在窗体的Open事件中加载连接到SQL Server的数据源。
前期绑定写法(必须引用ADO):- Private Sub Form_Open(Cancel As Integer)
- Dim cnn As ADODB.Connection
- Dim rst As ADODB.Recordset
- Dim strServer As String
- Dim strUserName As String
- Dim strPassword As String
- Dim strDatabase As String
-
- strServer = "(local)"
- strUserName = "sa"
- strPassword = "1234"
- strDatabase = "db_Csell"
-
- cnn.Open "Provider=SQLOLEDB" & _
- ";Data Source=" & strServer & _
- ";Initial Catalog=" & strDatabase, _
- strUserName, strPassword
- rst.CursorLocation = adUseClient
- rst.Open "SELECT * FROM 库存表;", cnn, adOpenKeyset, adLockOptimistic
- Set Me.Recordset = rst
- Set rst = Nothing
- Set cnn = Nothing
- End Sub
复制代码 后期绑定写法(不需要引用ADO):- Private Sub Form_Open(Cancel As Integer)
- Dim cnn As Object
- Dim rst As Object
- Dim strServer As String
- Dim strUserName As String
- Dim strPassword As String
- Dim strDatabase As String
-
- strServer = "(local)"
- strUserName = "sa"
- strPassword = "1234"
- strDatabase = "db_Csell"
-
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=SQLOLEDB" & _
- ";Data Source=" & strServer & _
- ";Initial Catalog=" & strDatabase, _
- strUserName, strPassword
- Set rst = CreateObject("ADODB.Recordset")
- rst.CursorLocation = 3
- rst.Open "SELECT * FROM 库存表;", cnn, 1, 3
- Set Me.Recordset = rst
- Set rst = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|