Solved

Create ODBC data source in VB ?

Posted on 1998-06-23
8
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Recommendation vb6 to vb.net or others 14 241
all the possible sequences for numbers 1,2,3,4,5,6,7,8,9,10 in groups of 5. 5 66
VB6 Enviroment 3 37
2 Global Vars, 1 List Box 4 34
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 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…

751 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