Solved

Create ODBC data source in VB ?

Posted on 1998-06-23
8
293 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
ID: 1464044
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
ID: 1464045
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
ID: 1464046
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
ID: 1464047
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:yowkee
ID: 1464048
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
ID: 1464049
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
ID: 1464050
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
ID: 1464051
YowKee,

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

Thank you.

netcool
koo@dapannet.com.my
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

861 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

20 Experts available now in Live!

Get 1:1 Help Now