Restored from previous forum. Originally posted by Rings.
With this code you can access every MDB or excel or other Database on the fly without add them to the ODBC-Control panel first.This snippet is another great converting from VB - by me, THE CODEGURU !
Code: Select all
;
;Create DSN on the fly for ODBC
;
; An example by Siegfried Rings (CodeGuru)
#ODBC_ADD_DSN = 1 ; Add Data source
#ODBC_CONFIG_DSN = 2 ; Configure (edit) Data source
#ODBC_REMOVE_DSN = 3 ; Remove Data source
Procedure MakeConnection(Driver.s, strAttributes.s)
Result=OpenLibrary(1,"ODBCCP32.DLL")
If Result
lpszDriver.s=Driver
; MessageRequester("Info",strAttributes,0)
MyMemory=AllocateMemory(Len(strAttributes))
CopyMemory(@strAttributes,MyMemory,Len(strAttributes))
For L=1 To Len(strAttributes )
If PeekB(MyMemory +l-1)=Asc(";")
PokeB(MyMemory +l-1,0)
EndIf
Next L
Result = CallFunction(1, "SQLConfigDataSource", 0, #ODBC_ADD_DSN, @lpszDriver.s, MyMemory)
FreeMemory(MyMemory)
CloseLibrary(1)
If Result
ProcedureReturn 1;MessageRequester("Info","DSN Created",0)
EndIf
EndIf
EndProcedure
Procedure DeleteConnection(Driver.s,DSN.s)
Result=OpenLibrary(1,"ODBCCP32.DLL")
If Result
lpszDriver.s=Driver
strAttributes.s = "DSN="+DSN
Result = CallFunction(1, "SQLConfigDataSource", 0, #ODBC_REMOVE_DSN, @lpszDriver.s, @strAttributes)
CloseLibrary(1)
If Result
ProcedureReturn 1;MessageRequester("Info","DSN Delete",0)
EndIf
EndIf
EndProcedure
;example:
Debug Makeconnection("Microsoft Access Driver (*.mdb)","Server=SomeServer; Description=Description For PurebasicODBC;DSN=PureBasic_DSN;DBQ=d:\biblio.mdb;UID=Rings;PWD=Siggi;")
Debug Makeconnection("Microsoft Excel Driver (*.xls)","DSN=Excel_TEST;Description=New_Excel_Data_Source;FileType=Excel97;DBQ=C:\\Telefon.xls;")
;or delete:
Debug DeleteConnection("Microsoft Access Driver (*.mdb)","PureBasic_DSN")
Edited by - Rings on 09 August 2002 12:44:23