Creating DSN

I have a VB6 program that uses a MSAccess MDB from a Dataenvironment connection. On my machine I know path to the MDB for all my data control sources. These tend to get hardwired into the program which is not a problem in development BUT on my clients machines I will not know where the MDB will be except that it will be in the App.Path, the Dataenviroment insists in putting the full path. If I use a DSN how can to create one on installation on the client's machine or is it possible to create one when the program starts ie

Pseudo code

Program start

 'Check if DSN exists

   if  test_DSN exists continue
   test_DSN =create_dsn( app.path & "test.mdb")
End if

Now the Dataenviroment1 can access the test_DSN

Is this possible or is there a better way to do this

Who is Participating?
bito_seConnect With a Mentor Commented:
You could specify the full path in the dataenvironment and then change the connectionstring using App.path in the dataenvironments initialize event. If your dataenvironment contains several connections you will of course have to repeat this procedure for every connection. Hope this is sufficient:

private sub denDataEnvironment_Initialize()

denDataEnvironment.cnnConnection.ConnectionString = "DBQ=" & App.Path & "\MyDatabase.mdb;Driver={Microsoft Access Driver (*.mdb)};"

end sub



If you look at a DSN file, you'll see that it's just a text file with parameters in it, you should be able to just create a file in the appropriate directory (wherever your DSN's sit) and you should be able to call it from there. I've not tried this, so just give it a go.... and good luck.
' paste this to a form
Option Explicit
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal HM As Integer, ByVal Rq As Integer, ByVal Driver As String, ByVal Connect As String) As Integer

Private Sub Form_Click()
    Dim rc As Integer, sName As String, sMDB As String
    sName = "MulgaDSN"
    sMDB = "c:\vb5\biblio.mdb"   ' ( app.path & "test.mdb")
    rc = SQLConfigDataSource(0, 1, "Microsoft Access Driver (*.mdb)", "DSN=" & sName & Chr(0) & "DBQ=" & sMDB & Chr(0) & Chr(0))
' return code 0 means failure
    If rc = 0 Then
        MsgBox "Could not configure data source."
    End If
End Sub

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

mulgaAuthor Commented:
That worked OK, BUT since doing that I have found installing ADO so problematic that I have decided to go back to DAO. A file that had an installation size of 1.9 MB blows out to 9.6 MB with DCOM and MDAC_TYP etc. Also had problems with some computers failing for some unknown reason . THe documentation is so shitty ----- Jeez!
Agreed Mulga - It doesn't help with the transition between DAO and ADO when it has so many 'features'...
how to insert values to the database from a command button click through insert statement

my insert statement is like this
insert into dept1 values (text1.text,'text2.text','text3.text')

Option Explicit
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Private Sub Form_Load()

conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=softprojex;Initial Catalog=pubs;Data Source=MICROSOFTTEST"
rs.Open "Select * from dept1", conn, adOpenKeyset
Set rs = Nothing
End Sub

please help me with code

thanks and regards

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.