I am undertaking a project but am hopelessly stuck. :cry: I have trawled the net, read books and followed tutorials but I just can't move forward. I would be really grateful if someone could help.

I need to create a sequential name and address file with the following options:

open a new file

open an existing file

add name and address info to open file

close a file

exit the program.

There are defined guidelines that I have so far stuck to. What I think I need to do is change my coding so I have an embedded db template that I can use to create a new db or open an existing db.

My code is as follows-

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        &n bsp;         &n bsp;         '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

Ty for looking.

What are you doing with the access database? It looks like to me, in your code, that you are mixing access databases with sequential files. Which do you want to use, an access database, or sequential files? You do this for access:

Set db = OpenDatabase(CMDialog1.FileName)

and then this:

OpenFileName = CMDialog1.FileName
Open OpenFileName For Append As #f

What is your ultimate goal? I can help you, but I need to know exactly what you are trying to accomplish.

ok ty your help is really appreciated as this is driving me nuts and taking up so much of my spare time. This is a project I am working through for an evening class (but we are allowed to get help from any source). The wording says a sequential name and address file, but the college I thought hinted that we needed a db to hold the name, address line 1, address line 2, address line 3 and postcode. We have been given various things that we have to put in our coding (including common dialog box) - hence most of what I have included.

Being a novice I am finding it so hard to make sense of the examples I am finding in books and on line.

My idea is that when I select file new it should allow me to open a new file (that I give a file name to) which is automatically linked to my textboxes. I then think that if I select file open I should be able to open any of my previously created files.

I have run into horrendous problems here and have had no luck even though I have tackled it in 3 or 4 different ways. Any help would be great.

I'd like to read the requirements. It sounds to me like you could use 1 file for each person that is a record. So, there is a file for rob smith, who lives at 2200 se ave florida.... and another file for robin smithers, who lives at 0022 nw blvd, michigan.... let me see the specifics, and I'll help you out.

Ok so as I said earlier I need to create a sequential name and address file with the following options:

open a new file

open an existing file

add name and address info to open file

close a file

exit the program.

My form has a drop down menu with file new, open, close and exit options.
It has 5 textboxes - Name, Address1, Address2, Address3, Postcode and an add command button. It also has a common dialog control.

So it then goes on to say

code form_load
set the string variable OpenFileName to empty

code mnuNew_click
set file filters for all files or txt files in the common dialog
set FilterIndex to default to all files
display open file dialog
assign FreeFile number to the variable TextFileNum
open selected file as output
set OpenFileName variable to the common dialog FileName

code mnuOpen_click
set file filters for all files or txt files in the common dialog
set FilterIndex to default to all files
display open file dialog
assign FreeFile number to the variable TextFileNum
open selected file as append
set OpenFileName variable to the common dialog FileName

code mnuClose_click
if a file is open
close file
set string variable OpenFileName to empty

code mnuExit_click
if a file is open close it
end the program

code mnuAdd_click
write the data in the textboxes to the file
clear the data from the textboxes

There are some basic things I have left out such as message boxes and enabled properties etc - these are in the code already.

Hope this makes sense.

Thanks

Can anyone help? As you can see by my coding I have done (I hope) the majority and am just stuck on how to put the information into a db or sequential file.

Am running out of time and ideas fast. Thanks

This has been fully commented. I would advise you to change some of the things in it, so that it fits the technique you are using, but, this should be a working copy of what you have been working on.

Thank you so much for your help so far. Just one question (for now anyway!) is there a way that when i open a file I have already created it can show the data stored in the correct textboxes. I have tried playing around with the code but I can't seem to hit upon the correct terminology to make it work.

Ty

Help me please somebody - I am in danger of taking a screwdriver to my laptop!!

This code kind of works in the mnuOpen

Dim TempStr As String ' Temporary Variable
Open OpenFileName For Input As #TextFileNum ' .. open the file
Do While Not EOF(1) ' While still records in file ..
Line Input #1, TempStr ' Read a line into the variable
txtName.Text = txtName.Text & TempStr & vbCrLf ' Display it in Text1
Loop

Close #1 ' All done: close the file

However 2 problems - my specification says open file as append and this only works if I open file for input. Also it lumps all of the inputted data together and displays it all in the one text box. Adding all of the text boxes just means all of the data is shown in all of the text boxes.

Dim TempStr As String ' Temporary Variable
Open OpenFileName For Input As #TextFileNum ' .. open the file
Do While Not EOF(1) ' While still records in file ..
Line Input #1, TempStr ' Read a line into the variable
txtName.Text = txtName.Text & TempStr & vbCrLf ' Display it in Text1
txtAddress1.Text = txtAddress1.Text & TempStr & vbCrLf
txtAddress2.Text = txtAddress2.Text & TempStr & vbCrLf
txtAddress3.Text = txtAddress3.Text & TempStr & vbCrLf
txtPostCode.Text = txtPostCode.Text & TempStr & vbCrLf
Loop

Close #1 ' All done: close the file

Thanks

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.