Solved

Create ODBC data source in VB ?

Posted on 1998-06-23
8
290 Views
Last Modified: 2012-06-27
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
Comment
Question by:netcool
  • 4
  • 4
8 Comments
 
LVL 4

Accepted Solution

by:
yowkee earned 100 total points
Comment Utility
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
 
LVL 5

Author Comment

by:netcool
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 
LVL 5

Author Comment

by:netcool
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 
LVL 5

Author Comment

by:netcool
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 
LVL 5

Author Comment

by:netcool
Comment Utility
YowKee,

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

Thank you.

netcool
koo@dapannet.com.my
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now