Access是一个集前台开发和后台数据库的开发管理软件。如果数据量较大的时候,我们会选择其他数据库作为后台数据库。
那么链接数据库我们可以选择链接表,ODBC等。下面的示例是自动创建ODBC源
Function gt_TestOdbc(rstDsn As String, rstrUser As String, rstrPassword As String, rstrTableName As String) As Boolean
On Error GoTo err_c
DoCmd.SetWarnings False
Dim Response As Integer
Dim connstr As String, mydb As DAO.Database
connstr = "ODBC;DSN=" & rstDsn & ";"
connstr = connstr & "UID=" & rstrUser & ";"
connstr = connstr & "PWD=" & rstrPassword & ";"
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
'
' Set dbs = CurrentDb()
' Set qdf = dbs.CreateQueryDef("")
'
' 'Replace <DSN> with the actual DSN that points to your SQL Server.
' qdf.connect = connstr ' "ODBC;DSN=<DSN>;UID=" & strUserId & "WD=" & _
' strPassword & ";DATABASE=pubs"
' qdf.ReturnsRecords = False
'
' 'Any SQL statement will work below.
' qdf.SQL = "SELECT * FROM " & rstrTableName
' qdf.Execute
'
'
Set mydb = DBEngine.Workspaces(0).OpenDatabase("", False, False, connstr)
' curpos = 0
gt_TestOdbc = True
Exit Function
err_c:
gt_TestOdbc = False
' MsgBox "数据库用户,口令错误,重新登录!", , "文具"
' DoCmd.RunSQL "delete from tblOdbcCfg"
' DoCmd.Quit
Exit Function
End Function