azkabancells
Instructor
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
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