Office学院 加入收藏
Office学院 Access Word Excel Powerpoint Wps Access Outlook Tags  
您现在的位置: Office学院 - Access - 用代码创建DSN

用代码创建DSN

添加记录: 用代码创建DSN 类别: Access 发布日期: 2007.05.10

Here is some code that I used in a distributed runtimeapp. This app needs to connect to a SQL Server database via ODBC, so it has to make sure that a System DSN exists for the linked tables. If someone runs this app, it will create the System DSN on the fly it if does not appear to exist.

'*********************** Code Start *************************** ' This code was originally written by Anthony S. Karre. ' It is not to be altered or distributed, ' except as part of an application. ' You are free to use it in any application, ' provided the copyright notice is left unchanged. ' ' Code Courtesy of
' Anthony S. Karre
' Const JDS_DSN_name = "MDTS" Const JDS_Server_name = "148.154.61.15" ' Raw IP address is used to avoid NT _ Domain name resolution probs. Private Declare Function RegEnumKeyEx Lib "advapi32.dll" _ Alias "RegEnumKeyExA" _ (ByVal hKey As Long, _ ByVal dwIndex As Long, _ ByVal lpName As String, _ lpcbName As Long, _ ByVal lpReserved As Long, _ ByVal lpClass As String, _ lpcbClass As Long, _ ByVal lpftLastWriteTime As String) As Long Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _ Alias "RegOpenKeyExA" _ (ByVal hKey As Long, _ ByVal lpSubKey As String, _ ByVal ulOptions As Long, _ ByVal samDesired As Long, phkResult As Long) As Long Private Declare Function SQLConfigDataSource Lib "odbccp32.dll" _ (ByVal hwndParent As Long, _ ByVal fRequest As Integer, _ ByVal lpszDriver As String, _ ByVal lpszAttributes As String) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long Const HKEY_LOCAL_MACHINE = &H80000002 Const ERROR_SUCCESS = 0& Const SYNCHRONIZE = &H100000 Const STANDARD_RIGHTS_READ = &H20000 Const STANDARD_RIGHTS_WRITE = &H20000 Const STANDARD_RIGHTS_EXECUTE = &H20000 Const STANDARD_RIGHTS_REQUIRED = &HF0000 Const STANDARD_RIGHTS_ALL = &H1F0000 Const KEY_QUERY_VALUE = &H1 Const KEY_SET_VALUE = &H2 Const KEY_CREATE_SUB_KEY = &H4 Const KEY_ENUMERATE_SUB_KEYS = &H8 Const KEY_NOTIFY = &H10 Const KEY_CREATE_LINK = &H20 Const KEY_READ = ((STANDARD_RIGHTS_READ Or _ KEY_QUERY_VALUE Or _ KEY_ENUMERATE_SUB_KEYS Or _ KEY_NOTIFY) And _ (Not SYNCHRONIZE)) Const REG_DWORD = 4 Const REG_BINARY = 3 Const REG_SZ = 1 Const ODBC_ADD_SYS_DSN = 4 Function Check_SDSN() ' Look for our System Data Source Name. If we find it, then great! ' If not, then let's create one on the fly. Dim lngKeyHandle As Long Dim lngResult As Long Dim lngCurIdx As Long Dim strValue As String Dim classValue As String Dim timeValue As String Dim lngValueLen As Long Dim classlngValueLen As Long Dim lngData As Long Dim lngDataLen As Long Dim strResult As String Dim DSNfound As Long Dim syscmdresult As Long syscmdresult = SysCmd(acSysCmdSetStatus, "Looking for System DSN " & JDS_DSN_name & " ...") ' Let's open the registry key that contains all of the ' System Data Source Names. lngResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _ "SOFTWARE\ODBC\ODBC.INI", _ 0&, _ KEY_READ, _ lngKeyHandle) If lngResult <> ERROR_SUCCESS Then MsgBox "ERROR: Cannot open the registry key " & _ "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI." & vbCrLf & vbCrLf & _ "Please make sure that ODBC and the SQL Server ODBC drivers have been installed." & vbCrLf & _ "Contact call your MDTS System Administrator for more information." syscmdresult = SysCmd(acSysCmdClearStatus) Check_SDSN = -1 End If ' Now that the key is open, Let's look among all of ' the possible system data source names for the one ' we want. lngCurIdx = 0 DSNfound = False Do lngValueLen = 512 classlngValueLen = 512 strValue = String(lngValueLen, 0) classValue = String(classlngValueLen, 0) timeValue = String(lngValueLen, 0) lngDataLen = 512 lngResult = RegEnumKeyEx(lngKeyHandle, _ lngCurIdx, _ strValue, _ lngValueLen, _ 0&, _ classValue, _ classlngValueLen, _ timeValue) lngCurIdx = lngCurIdx + 1 If lngResult = ERROR_SUCCESS Then ' Is this our System Data Source Name? If strValue = JDS_DSN_name Then ' It is! Let's assume everything is good and do nothing. DSNfound = True syscmdresult = SysCmd(acSysCmdClearStatus) End If End If Loop While lngResult = ERROR_SUCCESS And Not DSNfound Call RegCloseKey(lngKeyHandle) If Not DSNfound Then ' Our System Data Source Name doesn't exist, so let's ' try to create it on the fly. syscmdresult = SysCmd(acSysCmdSetStatus, "Creating System DSN " & JDS_DSN_name & "...") lngResult = SQLConfigDataSource(0, _ ODBC_ADD_SYS_DSN, _ "SQL Server", _ "DSN=" & JDS_DSN_name & Chr(0) & _ "Server=" & JDS_Server_name & Chr(0) & _ "Database=SvCvMarketing" & Chr(0) & _ "UseProcForPrepare=Yes" & Chr(0) & _ "Description=MDTS Database" & Chr(0) & Chr(0)) If lngResult = False Then MsgBox "ERROR: Could not create the System DSN " & JDS_DSN_name & "." & vbCrLf & vbCrLf & _ "Please make sure that the SQL Server ODBC drivers have been installed." & vbCrLf & _ "Contact your MDTS System Administrator for more information." syscmdresult = SysCmd(acSysCmdClearStatus) Check_SDSN = -1 End If End If syscmdresult = SysCmd(acSysCmdClearStatus) Check_SDSN = 0 End Function '*********************** Code End ***************************
顶一下
上一篇:用代码更改字段属性
下一篇:获得链接表路径
Tags: word

相关信息
  • 用代码更改字段属性
  • [绝招]Access人士必读
  • 在查询与窗体列表中控制小数位
  • [LWWVB教程]设计表的精义(第2课)
  • 如何用Access开发局域网软件(5)
  • 如何用Access开发局域网软件(4)
  • 如何用Access开发局域网软件(3)
  • 我的一个编程的经验
  • 如何用Access开发局域网软件(2)
  • 如何用Access开发局域网软件(1)
  • 我的ACCESS处女作,销售单统计
  • 用Access来进行数据分析随笔
  • 专栏作家-李伟文[lwwvb]
  • 本站聊天室开通
  • 一点感悟:关于与如何与用户交流
  • 获得链接表路径
  • 使用Access时必须避免的特殊字符
  • 使用 SetOption 来禁用警告消息
  • 如何以编程方式更新数据访问页链接
  • 绑定到存储过程或函数的窗体或报表没有应用WHERE条件
  • 从Excel导入或链接数据后Excel工作表的列格式丢失或更改
  • Access2003如何解析 VBA引用
  • 如何在Access数据库相关表中编辑记录
  • [LWWVB教程]设计表的精义(第4课)
  • 设计“编码”的几个经验
  • 根据企业特点定义月度范围
  • 关于我们
  • [FAQ]去掉XP窗体右上角的帮助框
  • [FAQ]如何查看VBA源程序
  • 如何以编程方式将条件格式设置应用于所有窗体控件














  • 生日密码
    血型分析
    生肖分析



    制作单位 Office学院 © 版权所有


    联系我们 关于我们 友情链接 站点地图 免责声明


    Office学院,致力于打造专业的OFFICE应用交流平台。