# Re: Add datasource in ODBC by VB code??

Posted by dumbjames on September 26, 1999 at 04:31:08:

In Reply to: Add datasource in ODBC by VB code?? posted by Joewall88 on September 24, 1999 at 12:01:29:

:Can i use vb code to create a new datasource in ODBC in run time rather than just manually establish the datasource in control panal of windows? Can any experts help me?

Good question you've asked. I have been wondering this too.
Sure you can. Just add this code to your module and call it as you would any function(please customize the code): (the orginal code is from VBPJ,VBTIPS 101)

' Declaration
Declare Function SQLConfigDataSource lib "OBDCCP32.DLL" (byval hwndParent as long, byval fRequest as long, byval lpszDriver as string, byval lpszAttributes as string) as long

' Function
public sub MakeODBCDataSource()
' Indent!!!
Const ODBC_ADD_DSN=1
Const ODBC_CONFIG_DSN=2
Const ODBC_REMOVE_DSN=3
Const vbAPINull as long = 0&
dim lngRet
' you got to check is there is already one dsn you set up
if getsetting(app.exename,"options","ODBCSetup","no")="no" then
dim sDeiver as string
dim sAttributes as string
sDriver="Microsoft Access Driver (*.mdb)"
sAttributes=sattributes & "DSN=MyDSN" & chr(0)
sAttributes=sattributes & "DBQ=C:\Temp\Myfile.mdb" & chr(0)
lngRet=SQLCONFIGDATASOURCE(VBAPINULL,ODBC_ADD_DSN,Sdriver,sattributes)
savesetting app.exename,"options","odbcsetup","yes"
endif
end sub

Follow Ups: