DAO/链接表/ADO/ADP
[源创技巧]在ACCESS中使用代码来自动创建 ODBC
2013-07-08 11:07:55
可以在ODBC源中创建ODBC,但程序员也可以 使用代码来自动创建 ODBC,这样可能更新可以灵活控制
  1. Function gt_TestOdbc(rstDsn As String, rstrUser As String, rstrPassword As String, rstrTableName As String) As Boolean
  2.  
  3. On Error GoTo err_c
  4.  
  5. DoCmd.SetWarnings False
  6.  
  7. Dim Response As Integer
  8. Dim connstr As String, mydb As DAO.Database
  9. connstr = "ODBC;DSN=" & rstDsn & ";"
  10. connstr = connstr & "UID=" & rstrUser & ";"
  11. connstr = connstr & "PWD=" & rstrPassword & ";"
  12.  
  13. Dim dbs As DAO.Database
  14. Dim qdf As DAO.QueryDef
  15. '
  16. ' Set dbs = CurrentDb()
  17. ' Set qdf = dbs.CreateQueryDef("")
  18. '
  19. ' 'Replace <DSN> with the actual DSN that points to your SQL Server.
  20. ' qdf.connect = connstr ' "ODBC;DSN=<DSN>;UID=" & strUserId & "WD=" & _
  21. ' strPassword & ";DATABASE=pubs"
  22. ' qdf.ReturnsRecords = False
  23. '
  24. ' 'Any SQL statement will work below.
  25. ' qdf.SQL = "SELECT * FROM " & rstrTableName
  26. ' qdf.Execute
  27. '
  28. '
  29.  
  30. Set mydb = DBEngine.Workspaces(0).OpenDatabase("", False, False, connstr)
  31. ' curpos = 0
  32. gt_TestOdbc = True
  33. Exit Function
  34. err_c:
  35.  
  36. gt_TestOdbc = False
  37.  
  38. ' MsgBox "数据库用户,口令错误,重新登录!", , "文具"
  39. '删除表配置信息
  40. ' DoCmd.RunSQL "delete from tblOdbcCfg"
  41. ' DoCmd.Quit
  42. Exit Function
  43. End Function