I personally often do some database programs, so I have a deep understanding of the problem of how to interface between programs and the database, because when VB connects to the database, it is usually static, that is, the path where the database is stored is fixed, such as When using VB's DATA, adodc, DataEnvironment, etc. to connect to the database, if the path to store the database is changed, the path will not be found, which is really annoying.
The author's solution is to use app.path to solve this problem.
1. Use the data control to connect to the database, as follows:
Put in the form_load() process:
PRivateform_load()
DimstrAsString' definition
str=App.Path
IfRight(str,1)<>"/"Then
str=str "/"
EndIf
data1.databasename=str&"/database name"
data1.recordsource="data table name"
data1.refresh
subend
The meaning of these sentences is to open the database in the directory where the current program is running.
You just need to make sure that your database is in the directory where your program is located.
2. Use adodc (ADODataControl) for database link:
privateform_load()
DimstrAsString' definition
str=App.Path
IfRight(str,1)<>"/"Then
str=str "/"
EndIf
str="Provider=Microsoft.Jet.OLEDB.3.51;PersistSecurityInfo=False;DataSource="&str&"/tsl.mdb"
Adodc1.ConnectionString=str
Adodc1.CommandType=adCmdText
Adodc1.RecordSource="select*fromtable3"
Adodc1.Refresh
endsub
3. Use DataEnvironment for database linking
You can put in the process:
OnErrorResumeNext
IfDataEnvironment1.rsCommand1.State<>adStateClosedThen
DataEnvironment1.rsCommand1.Close'If open, close
EndIf
'i=InputBox("Please enter the friend number:", "Enter")
'Ifi=""ThenExitSub
DataEnvironment1.Connection1.OpenApp.Path&"/userdatabase/tsl.mdb"
DataEnvironment1.rsCommand1.Open"select*fromtable3where number='"&i&"'"
'SetDataReport2.DataSource=DataEnvironment1
'DataReport2.DataMember="command1"
'DataReport2.show
endsub
4. Programming using ADO (ActiveXDataObjects):
Establish a connection:
dimconnasnewadodb.connection
dimrsasnewadodb.recordset
dimstr
str=App.Path
IfRight(str,1)<>"/"Then
str=str "/"
EndIf
str="Provider=Microsoft.Jet.OLEDB.3.51;PersistSecurityInfo=False;DataSource="&str&"/tsl.mdb"
conn.openstr
rs.cursorlocation=aduseclient
rs.open"data table name",conn,adopenkeyset.adlockpessimistic
Close the database after use:
conn.close
setconn=nothing->