Solved

Create ODBC data source in VB ?

Posted on 1998-06-23
8
297 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

828 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