Hello all!
:-/
I am creating a program which reads data from a database. How can I change the connection string to format anyone who would use this program on there computer?

My current connection string is made to specifically read from my folder(s)...
conn.ConnectionString = "C:\Documents and Settings\servername.\Desktop\Pamela\CheckReader\Database\CheckInfo.mdb"

Thanks much!
Kudos!!

Recommended Answers

All 3 Replies

you could put up a dialog box asking user to open database they wish to open.

First create a file (say myfile.txt) in C drive of each computer where your program access the dataBase with the following lines of information in quotes.

DataSource = "ComputerName"
DBUser = "UserName"
DBPassword = "Password"
Catalog = "Databasname"
Provider = "SQLOLEDB.1"

In your VB project file under
Private Sub Form_load()
'Open this File myfile.txt and read each line and store the values in 'variables such as

Dim string_datasorce,string_dbuser,string_password,string_catalog,string_provider,LineRead as String

'Two Loop Indexes i and j

Dim i, j as integer
Open "c:\myfile.txt" For Input As #1

'Put it in a loop to read each characters of the values in quotes

Do While Not EOF(1)
Line Input #1, LineRead
If Mid(LineRead,1,10) = "DataSource" then
'read the consecutive characters up to the last quote say upto 100 'characters
For i = 11 to 100
if Mid(LineRead, i,1) = Char(34) then
For j = i + 1 to 100
if Mid(LineRead, j, 1) = Char(34) then
string_datasource = Mid(LineRead, i + 1, j - 1 - i)
Exit For
End If
Next j
Endif
Next i
Else
' Now the next line containing DBUser
If Mid(LineRead, 1,6) = "DBUser" then
'Same two loops as before indexing i and j
For i = 8 to 100
' ...............
' string_dbuser = Mid(LineRead ...........)
'.............
' Inside the do loop read in to all string Variables the data as I have 'shown it in the first variable
Loop
Close #1
'Now you are ready with the data in the corresponding variables
'Now Comes the connection string and recordset. You Can Place 'them as Global Variable

Dim MyConnectionString As New ADODB.Connection
Dim MyCursor as New ADODB.Recordset

' In your program where you want to access the database simply 'place the following codes

MyConnectionString.ConnectionString = "Provider = string_provider;UID = string_dbuse;PWD = string_password;Data Source = string_datasource;Initial Catalog=string_catalog

MyConnectionString.Open
MyCursor.ActiveConnection = MyConnectionString
Ctype = adOpenStatic
CLocation = adUseServer
MyCursor.CursorLocation = CLocation
MyCursor.CursorType = Ctype

If MyCursor.State = adStateOpen then
MyCursor.Close
End If

MyCursor.Open "SELECT * FROM MYTABLE............

If MyCursor.State = adStateOpen then
MyCursor.Close
End If

'That's all
'HAPPY PROGRAMING

check this out :-

conn.ConnectionString = app.path & "\CheckInfo.mdb"

make sure the .mdb file does exist in the same folder as ur project files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.