用ACCESS2010给别人做个简单的前端界面,其中碰到一些问题,都解决了,总结一下解决方法。
一、自动建立ODBC数据源。
用户不会建立数据源,那么程序实现自动创建。自动创建有很多方法,不过不外乎是修改注册表,我觉得最好的方法是以下方法。调用winapi函数
Option Compare Database Option Explicit Private Const REG_SZ = 1 'Constant for a string variable type. Private Const HKEY_LOCAL_MACHINE = &H80000002 '这里要特别注意,用户必须要有修改此键值的权限才可以进行操作,如果没有则下面返回错误值5 Private Const HKEY_CURRENT_USER = &H80000001 '我就是没有相应的权限,所以我只能修改USER下面的信息,最后结果是一样的。 Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _ "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _ phkResult As Long) As Long Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _ "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _ cbData As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long Sub CreateOdbc() Dim DataSourceName As String Dim DatabaseName As String Dim Description As String Dim DriverPath As String Dim DriverName As String Dim LastUser As String Dim Regional As String Dim Server As String Dim lResult As Long Dim hKeyHandle As Long 'Specify the DSN parameters. DataSourceName = "XX" DatabaseName = "XXX" Description = "XXXX" DriverPath = Environ("SYSTEMROOT") & "\system32\sqlsrv32.dll" 'vba里通常用environ("systemroot")来获取..\system32目录 LastUser = "XX" Server = "XX.XX.XX.XX" DriverName = "SQL Server" lResult = RegCreateKey(HKEY_CURRENT_USER, "SOFTWARE\ODBC\ODBC.INI\" & _ DataSourceName, hKeyHandle) lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _ ByVal DatabaseName, Len(DatabaseName) * 2) '这里特别注意,如果数据库名称是汉字,那么缓冲区必须是2倍的长度,1个汉字2个字节 lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _ ByVal Description, Len(Description) * 2) lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _ ByVal DriverPath, Len(DriverPath)) lResult = RegSetValueEx(hKeyHandle, "LastUser", 0&, REG_SZ, _ ByVal LastUser, Len(LastUser)) lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _ ByVal Server, Len(Server)) lResult = RegCloseKey(hKeyHandle) 'Open ODBC Data Sources key to list the new DSN in the ODBC Manager. 'Specify the new value. 'Close the key. lResult = RegCreateKey(HKEY_CURRENT_USER, _ "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle) lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _ ByVal DriverName, Len(DriverName)) lResult = RegCloseKey(hKeyHandle) End Sub
二、检测ODBC链接表是否可以打开,那么通过ADO连接一下即可。
Public Function Is_DBConnect() As Boolean Dim Myconn As New ADODB.Connection On Error GoTo Err Myconn.ConnectionString = "DSN=XX;UID=XXX;Pwd=XXXXXX;" Myconn.ConnectionTimeout = 10 Myconn.CursorLocation = adUseClient Myconn.Open Is_DBConnect = True Exit Function Err: Is_DBConnect = False Set Myconn = Nothing End Function
三、链接表以及隐藏表
DoCmd.TransferDatabase acLink, "<Sql Database>", "ODBC;DRIVER=SQL Server;Server=IP;UID=;PWD=;DATABASE=", acTable, "表", "表" '链接一个表。可以省略上述建立ODBC的麻烦。 Application.SetHiddenAttribute acTable, "表", True '隐藏一张表
四、重新加载窗体时,保持原始大小。
将窗体的弹出方式设置为“是”。这样重新加载的时候,不会最大化。