Another noob question here.. Im a frshman programming student and I don't have the best instructor. I've just finish doing a simple inventory system Vb6 + MS Access database. My question is how can I save the database path? because whenever I change computer from Home to School I need to reconnected all the database connections.. or else a "/db/.mdb is not a valid path" error pops out.. I still can't figure it out how to set a database path... I hope you kinda understand my question. sorry for my bad english. I really need some help or guidance.. thanks
You have several options. First and simplest is to have the database and the application in the same directory. Then the connection path is ".\db.mdb" and it will always look in current directory, no matter where you execute from.
If that's not an option (as in you have to point to a shared directory), then you could do some error trapping in your app so if the first connection attempt fails it tries the second connection. You just have to have constants to contain the various paths in question.
Next, if you know how to do text file reading/writing you could save the path into a config file and have your app read the config file before connecting. Then you just have to change the path in the config file when you go from home to school.
Next after that (and probably hardest) is put an entry in an INI file and use GetPrivateProfileString to read the entry. You'll have to Google that to find out how to use it properly. I don't really recommend this unless you're fairly advanced in VB.
Hope those ideas help. Good luck!
My question is how can I save the database path? because whenever I change computer from Home to School I need to reconnected all the database connections.. or else a "/db/.mdb is not a valid path" error pops out.. I still can't figure it out how to set a database path...
Show the line that is causing the error and it would be easier to help.
BitBlt has a good suggestion: trapping the error. Your home path for the database file and the school path shouldn't change and you should know the path names.
Private Sub ConnectToDB() On Error GoTo DBCONNECT_ERR Dim strDbPath as String strDbPath = strHomePath Set rst = New ADODB.Recordset Set cn = New ADODB.Connection With cn .Provider = "Microsoft.Jet.OLEDB.4.01" .Open strDbPath & "\db\My.mdb", ...., ,, End with .... .... Exit Sub DBCONNECT_ERR: if strDbPath = strHomePath then strDbPath = strSchoolPath Else strDbPath = strHomePath End if cn.open strDbPath & "\db\My.mdb" Resume Next End Sub
This error trap just assumes that the error will be a bad path. Uuually, you want to trap the error, look at the err.Number, and then decide on what course of action to take based on the error code. But this should give you an idea. This example assumes you're at home and that you've already hard coded values for the home path and the school path in a variable that's available for the ConnectToDb sub routine.