Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Create ODBC data source in VB ?

How can i create an OBBC data source in VB programming ?
The problem i face is when i copy a database(.mdb) into new location and with new name, i want  Vb
to create the data source in ODBC as well . Is that possible  ? If not what other method can i use to
create ODBC data source without going into control panel and open the ODBC setup program and
configure it .

Your help will be appreciated. Thank you.
0
netcool
Asked:
netcool
  • 4
  • 4
1 Solution
 
yowkeeCommented:
netcool,
 
  You could use API SQLConfigDataSource (in odbcinst.dll) to implement this. MS KB has an article of this issue(Q171146).

  Check the article at
http://support.microsoft.com/support/kb/articles/Q171/1/46.asp

  Regards.
0
 
netcoolAuthor Commented:
yowkee ,

Your example code work good for me. However i could not add the the database to it.
Even i put database=c:\imcat\data\main.mdb to the string in the  attribute.
Can yuo answer me how can i do that ????

Netcool
0
 
yowkeeCommented:
Netcool,

  For adding DSN to access database, you need to change the driver name to pass into third parameter of SQLConfigDataSource. You could get the driver name from ODBC driver manager. When you click add button to add a DSN, it would list out all avilable driver name.

eg. (The code I modified from KB article mentioned)
----
Private Const ODBC_ADD_DSN = 1        ' Add data source
Private Const ODBC_CONFIG_DSN = 2     ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3     ' Remove data source
Private Const vbAPINull As Long = 0&  ' NULL Pointer

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
    (ByVal hwndParent As Long, ByVal fRequest As Long, _
    ByVal lpszDriver As String, ByVal lpszAttributes As String) _
    As Long

Private Sub Command1_Click()

    Dim intRet As Long
    Dim strDriver As String
    Dim strAttributes As String

    'Set the driver to SQL Server because it is most common.
    strDriver = "Microsoft Access Driver (*.mdb)"

    'Set the attributes delimited by null.
    'See driver documentation for a complete
    'list of supported attributes.
    'strAttributes = "SERVER=SomeServer" & Chr$(0)
    strAttributes = "DESCRIPTION=Temp DSN" & Chr$(0)
    strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
    strAttributes = strAttributes & "DATABASE=C:\Program Files\DevStudio\vb\biblio.mdb" & Chr$(0)
    'strAttributes = strAttributes & "UID=Admin" & Chr$(0)
    'strAttributes = strAttributes & "PWD=" & Chr$(0)
    'To show dialog, use Form1.Hwnd instead of vbAPINull.
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
    strDriver, strAttributes)
    If intRet Then
        MsgBox "DSN Created"
    Else
        MsgBox "Create Failed"
    End If

End Sub

------

Regards.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
netcoolAuthor Commented:
yowkee :

I have tried your example and make sure there is no mistake in my typing or anything. However, the program still not able to select the database as specify in the StrAttribute....
When i open the ODBC manager and it only shown the DSN, Description, and Microsoft
Access Driver but the database select show nothing. why...?  
0
 
yowkeeCommented:
netcool,

  May I know what version of VB you are using? And develop under which platform? (I should ask this before answer :P ). And when you run the ODBC manager, what is the text that ODBC show the MS Access driver(is it just like "Microsoft Access Driver (*.mdb)") ?

  Regards.
0
 
netcoolAuthor Commented:
Yowkee,

I am using VB 5 Enterprise Edition and  the paltform i am suing is win95.
Yes, it show ( Microsoft Access Driver (*.mdb) ) , Ver 3.50.360200, File = odbcjt32.dll


Thank.


0
 
yowkeeCommented:
netcool,

  I get what you mean. Sorry that I didn't really check for that thing. :) And now I found out we should change the Keyword 'Database' to 'DBQ'.

  Change this line
    strAttributes = strAttributes & "DATABASE=C:\Program Files\DevStudio\vb\biblio.mdb" & Chr$(0)
 
  To
    strAttributes = strAttributes & "DBQ=C:\Program Files\DevStudio\vb\biblio.mdb" & Chr$(0)

  Regards.
0
 
netcoolAuthor Commented:
YowKee,

Yes...yes....: )  It work perfectly.....and i can finish my project today.

Thank you.

netcool
koo@dapannet.com.my
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now