Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access db/ Common Dialog/ errors in code

Status
Not open for further replies.

azkabancells

Instructor
May 8, 2005
4
GB
I am writing an address book in vb6. My version of VB uses DAO 3.51 - I can't load the upgrade from MS. I have tried running the following with an Access 2000 and 97 file but both give me the following meassage runtime error 3343 unrecognizable database format.

Here is my code, some more questions at the end....

Dim TextFileNum As Integer
Dim OpenFileName As String
Dim db As Database
Dim rs As Recordset

Private Sub Form_Load()
OpenFileName = Empty
mnuClose.Enabled = False

End Sub

Private Sub mnuNew_Click()
f = FreeFile
CMDialog1.Filter = "all files|*.*|text|*.txt"
CMDialog1.FilterIndex = 0
CMDialog1.InitDir = App.Path
CMDialog1.ShowOpen
MsgBox "You selected: " + CMDialog1.FileName
Set db = OpenDatabase(CMDialog1.FileName)
Set rs = db.OpenRecordset("Table1")
rs.AddNew
rs![Name] = txtName.Text
rs![Address1] = txtAddress1.Text
rs![Address2] = txtAddress2.Text
rs![Address3] = txtAddress3.Text
rs![Postcode] = txtPostcode.Text
TextFileNum = FreeFile
OpenFileName = CMDialog1.FileName
Open OpenFileName For Output As #f
cmdAdd.Visible = True
mnuNew.Enabled = False
mnuOpen.Enabled = False
mnuClose.Enabled = True

End Sub

Private Sub mnuOpen_Click()

f = FreeFile
'CMDialog1.CancelError = True
'On Error GoTo Errhandler 'set error for cancel button
CMDialog1.Filter = "all files|*.*|text|*.txt"
CMDialog1.FilterIndex = 0
CMDialog1.InitDir = App.Path
CMDialog1.ShowOpen
MsgBox "You selected: " + CMDialog1.FileName
Set db = OpenDatabase(CMDialog1.FileName)
Set rs = db.OpenRecordset("Table1")
rs.Edit
txtName.Text = rs![Name]
txtAddress1.Text = rs![Address1]
txtAddress2.Text = rs![Address2]
txtAddress3.Text = rs![Address3]
txtPostcode.Text = rs![Postcode]
rs.MoveFirst

TextFileNum = FreeFile
OpenFileName = CMDialog1.FileName
Open OpenFileName For Append As #f
cmdAdd.Visible = True
mnuNew.Enabled = False
mnuOpen.Enabled = False
mnuClose.Enabled = True

Exit Sub

'Errhandler:
'MsgBox "You clicked the cancel button"
'Exit Sub

End Sub

Private Sub mnuClose_Click()

'if

cmdAdd.Visible = False

'then

mnuOpen.Enabled = True
mnuNew.Enabled = True
mnuClose.Enabled = False

'Else

MsgBox "001 File not open", vbOKOnly, "Error Details"

End Sub

Private Sub mnuExit_Click()
rs.Close
Close (f)
End
End Sub

Private Sub cmdAdd_Click()
'rs.AddNew
rs![Name] = txtName.Text
rs![Address1] = txtAddress1.Text
rs![Address2] = txtAddress2.Text
rs![Address3] = txtAddress3.Text
rs![Postcode] = txtPostcode.Text
rs.Update

End Sub

It is not finished yet hence the rem statements. I have strict coding guidelines and I am sure there are things in my code that could be streamlined - however most of it is a requirement of the end user.

Also this opens up a db I have previously created in access. Is there an easy way to code so that when I click menu new it creates a new db with the fields name, address1, address2, address3, postcode automatically. Then upon selecting menu open I have a choice of all previously created db's.

I am still struggling to work out the logic behind the coding so I would be really grateful if you could give me an answer in newbie language.

Many thanks

Harry
 
upgrade to dao 3.6

Try using service packing vb as there was a problem with the prev version.
 
thanks - however surely if I convert the db to access97 format it should work fine - as I have done on previous projects using the data control. However it isn't working properly.

Any ideas on the other question??

"Also this opens up a db I have previously created in access. Is there an easy way to code so that when I click menu new it creates a new db with the fields name, address1, address2, address3, postcode automatically. Then upon selecting menu open I have a choice of all previously created db's."

Thanks
 
Since you are using DAO, look in to the CreateDatabase function.

Once you create the new database, you can use SQL to create a table. To get you started, the command would be something like "Create Table Address (Name Text(20), Address1 Text(20), NextField FieldType, etc...)"

If you make sure all databases are located in the same directory, then you should be able to use the CommonDialog to allow the user to select the database for opening an existing one.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top