Create an ODBC entry for a MS Access database

From NSIS Wiki
Jump to navigationJump to search
Author: preisl (talk, contrib)


The Script

;
; Create an ODBC entry for MS Access database
;
; Requires: ODBCEntryExists
;    Input: Name of the ODBC entry
;           Path to the MS Access database
;   Output: FALSE,TRUE
;
; Usage:
;
;    Push "ODBCTEST"
;    Push "c:\program files\mdb\test.mdb"
;    Call CreateAccessODBCEntry
;    Pop $1
;    MessageBox MB_OK|MB_ICONINFORMATION "Access ODBC entry 'ODBCTEST' created: $1"
;
 
Function CreateAccessODBCEntry
 
  Exch $R1            ; Path to the MS Access database
  Exch
  Exch $R2            ; Name of the ODBC entry
 
  ; Save R0 on the stack
  Push $R0
 
  ; Check whether the ODBC entry already exists
  Push $R2
  Call ODBCEntryExists
  Pop $R0
  MessageBox MB_OK $R0
  StrCmp $R0 "TRUE" AlreadyExists
 
  ; Create the necessary registry keys
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2" "DBQ" "$R1"
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2" "Driver" "$SYSDIR\odbcjt32.dll"
  WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2" "DriverId" 25
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2" "FIL" "MS Access;"
  WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2" "SafeTransactions" 0
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2" "UID" ""
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2\Engines\Jet" "ImplicitCommitSync" ""
  WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2\Engines\Jet" "MaxBufferSize" 2048
  WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2\Engines\Jet" "PageTimeout" 5
  WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2\Engines\Jet" "Threads" 3
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\$R2\Engines\Jet" "UserCommitSync" "Yes"
  WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "$R2" "Microsoft Access Driver (*.mdb)"
 
  StrCpy $R1 "TRUE"
  Goto Done
 
  AlreadyExists:
    StrCpy $R1 "FALSE"
    Goto Done
 
  Done:
 
  ; Restore R0-R2
  Pop $R0
  Pop $R2
  Exch $R1
 
FunctionEnd