Link to home
Start Free TrialLog in
Avatar of DrD
DrD

asked on

Configure Access Database ODBC using VB6 Code

Right now I have an application that uses an Access database and an ODBC connection using VB6.

I currently have to set up the ODBC configuration using Control Panel/ Administrative Tools/Data Sources (ODBC)/User DSN etc in Windows 2000.

This means that when I distribute the application I will have to get other users to go into Control Panel, and configure the ODBC and set the DSN for the proper database.

Has anyone figured out a way to do this from within VB6. It sounds like an API call(?) will be required...

I would like to have the user select the database and then have VB configure the DSN name and driver using code...
Avatar of MYLim
MYLim

Try This:

Option Explicit


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 ODBC_ADD_SYS_DSN = 4    ' Add a new system data source.

'ODBC_CONFIG_SYS_DSN: Modify an existing system data source.

'ODBC_REMOVE_SYS_DSN: Remove an existing system 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



Public Function AddODBCDSN()

    Dim strDriver As String
    Dim strAttributes As String
    Dim intRet As Long
     
   'since i do not have SQL Server installed....
    strDriver = "Microsoft Access Driver (*.mdb)"      'Set the attributes delimited by null.
    'See driver documentation for a complete
    'list of supported attributes.
     
   'You should use this
    'strDriver = "SQL Server"
    strAttributes = "SERVER=SomeServer" & Chr$(0)
    strAttributes = strAttributes & "DESCRIPTION=Test DSN" & Chr$(0)
    strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
    strAttributes = strAttributes & "DATABASE=c:\test.mdb" & Chr$(0)
    'strAttributes = strAttributes & "UID=" & Chr$(0)
    'strAttributes = strAttributes & "PWD=" & Chr$(0)
       
   'To show dialog, use Form1.Hwnd instead of vbAPINull.
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, strDriver, strAttributes)
    If intRet Then
        MsgBox "DSN Created"
    Else
        MsgBox "Create Failed"
     
   End If
End Function

ASKER CERTIFIED SOLUTION
Avatar of MYLim
MYLim

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi DrD,

Have you considered using a dsnless connection?

Invoke browse for file dialog, then build the connect string and open the connection
http://www.able-consulting.com/MDAC/ADO/Connection/ODBC_DSNLess.htm#ODBCDriverForAccess

Alan :)
Hi DrD. I think you only need to create the dsn once and that's during installation. So, try creating this with your setup program. I suggest using Inno Setup. It's free.

http://www.jrsoftware.org/isinfo.php

Visit the vb tutorial here:

http://www.jrsoftware.org/iskb.php?vb

or just run this software to create the packaging script:

http://www.randem.com/innoscript/innoscript.htm

and use this tool to create the script for your DSN:

http://www.istool.org/

It might not be the quick and easy way but when you start getting PDW problems, you'll end up using other installation softwares like the ones above. But then you could always find out for yourself...

Good Luck! :0)
Avatar of DrD

ASKER

Wow!

I sure got some responses to this one.. I'll get back as soon as I can. It will take me a day or so to work through all of them...

Thanks!

David (drD)

I need to know ehat lines of code i need to connect to a microsoft access databse, then how to add records to the databse.thank you
this code works, well it puts up the DSN entry, but it will not select the database at all. that is, it puts up a DSN that points to nothing

my parameters are as follows --

      strDriver = "Microsoft Access Driver (*.mdb)"
      strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
      strAttributes = strAttributes & "DSN=RM TEMP" & Chr$(0)
      strAttributes = strAttributes & "DATABASE=" & App.Path & "RM.mdb" & Chr$(0)

      intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)

anyone help?
is there one thing missing here???

strAttributes = strAttributes & "DATABASE=" & App.Path & "RM.mdb" & Chr$(0)

shouldnt that be...

strAttributes = strAttributes & "DATABASE=" & App.Path & "\RM.mdb" & Chr$(0)

The backslash isn't added by default to the app.path
This works -

strAttributes = strAttributes & "Dbq=" & App.Path & "\RM.mdb" & Chr$(0)