Pages

Banner 468

Minggu, 23 November 2008

CREATE DSN LEWAT SCRIPT VB

 
Sub MakeDSN(ByVal sDSN As String, ByVal sDriver As String, _
ByVal sDBFile As String, ByVal lAction As Long)

Dim sAttributes As String
Dim sDBQ As String
Dim lngRet As Long

Dim hKey As Long
Dim regValue As String
Dim valueType As Long

' query the Registry to check whether the DSN is already installed
' open the key
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\ODBC\ODBC.INI\" & sDSN, 0, _
KEY_ALL_ACCESS, hKey) = 0 Then
' zero means no error => Retrieve value of "DBQ" key
regValue = String$(1024, 0)
' Allocate Variable Space
If RegQueryValueEx(hKey, "DBQ", 0, valueType, regValue, _
Len(regValue)) = 0 Then
' zero means OK, so we can retrieve the value
If valueType = REG_SZ Then
sDBQ = Left$(regValue, InStr(regValue, vbNullChar) - 1)
End If
End If
' close the key
RegCloseKey hKey
End If

' Perform the action only if we're adding a DSN that doesn't exist
' or removing and existing DSN
'If (sDBQ = "" And lAction = ODBC_ADD_DSN) Or (sDBQ <> "" And lAction = _
ODBC_REMOVE_DSN) Then

' check that the file actually exists
' If Len(Dir$(sDBFile)) = 0 Then
' MsgBox "Database file doesn't exist!", vbOKOnly + vbCritical
' 'Exit Sub
' End If
sAttributes = "DSN=" & sDSN & vbNullChar & "DBQ=" & sDBFile & vbNullChar & "PWD=" & "" & vbNullChar
lngRet = SQLConfigDataSource(0&, lAction, sDriver, sAttributes)
MDIMenu.Show
' End If
End Sub


'#########

' cara memanggilnya
'MakeDSN "Sekolah", "Microsoft Access Driver (*.mdb)", App.Path & "\database\" & "sekolah.mdb", 3 ' untuk menghapus
'MakeDSN "Sekolah", "Microsoft Access Driver (*.mdb)", App.Path & "\database\" & "sekolah.mdb", 1 ' untuk create dsn baru