vb6/common dialog/access db

Reply

Join Date: May 2005
Posts: 6
Reputation: azkabancells is an unknown quantity at this point 
Solved Threads: 0
azkabancells's Avatar
azkabancells azkabancells is offline Offline
Newbie Poster

vb6/common dialog/access db

 
0
  #1
May 21st, 2005
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-

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim TextFileNum As Integer
  2. Dim OpenFileName As String
  3. Dim db As Database
  4. Dim rs As Recordset
  5.  
  6. Private Sub Form_Load()
  7. OpenFileName = Empty
  8. mnuClose.Enabled = False
  9.  
  10. End Sub
  11.  
  12. Private Sub mnuNew_Click()
  13. f = FreeFile
  14. CMDialog1.Filter = "all files|*.*|text|*.txt"
  15. CMDialog1.FilterIndex = 0
  16. CMDialog1.InitDir = App.Path
  17. CMDialog1.ShowOpen
  18. MsgBox "You selected: " + CMDialog1.FileName
  19. Set db = OpenDatabase(CMDialog1.FileName)
  20. Set rs = db.OpenRecordset("Table1")
  21. rs.AddNew
  22. rs![Name] = txtName.Text
  23. rs![Address1] = txtAddress1.Text
  24. rs![Address2] = txtAddress2.Text
  25. rs![Address3] = txtAddress3.Text
  26. rs![Postcode] = txtPostcode.Text
  27. TextFileNum = FreeFile
  28. OpenFileName = CMDialog1.FileName
  29. Open OpenFileName For Output As #f
  30. cmdAdd.Visible = True
  31. mnuNew.Enabled = False
  32. mnuOpen.Enabled = False
  33. mnuClose.Enabled = True
  34.  
  35. End Sub
  36.  
  37. Private Sub mnuOpen_Click()
  38.  
  39. f = FreeFile
  40. 'CMDialog1.CancelError = True
  41. 'On Error GoTo Errhandler &n bsp; &n bsp; 'set error for cancel button
  42. CMDialog1.Filter = "all files|*.*|text|*.txt"
  43. CMDialog1.FilterIndex = 0
  44. CMDialog1.InitDir = App.Path
  45. CMDialog1.ShowOpen
  46. MsgBox "You selected: " + CMDialog1.FileName
  47. Set db = OpenDatabase(CMDialog1.FileName)
  48. Set rs = db.OpenRecordset("Table1")
  49. rs.Edit
  50. txtName.Text = rs![Name]
  51. txtAddress1.Text = rs![Address1]
  52. txtAddress2.Text = rs![Address2]
  53. txtAddress3.Text = rs![Address3]
  54. txtPostcode.Text = rs![Postcode]
  55. rs.MoveFirst
  56.  
  57. TextFileNum = FreeFile
  58. OpenFileName = CMDialog1.FileName
  59. Open OpenFileName For Append As #f
  60. cmdAdd.Visible = True
  61. mnuNew.Enabled = False
  62. mnuOpen.Enabled = False
  63. mnuClose.Enabled = True
  64.  
  65. Exit Sub
  66.  
  67. 'Errhandler:
  68. 'MsgBox "You clicked the cancel button"
  69. 'Exit Sub
  70.  
  71. End Sub
  72.  
  73. Private Sub mnuClose_Click()
  74.  
  75. 'if
  76.  
  77. cmdAdd.Visible = False
  78.  
  79. 'then
  80.  
  81. mnuOpen.Enabled = True
  82. mnuNew.Enabled = True
  83. mnuClose.Enabled = False
  84.  
  85. 'Else
  86.  
  87. MsgBox "001 File not open", vbOKOnly, "Error Details"
  88.  
  89. End Sub
  90.  
  91. Private Sub mnuExit_Click()
  92. rs.Close
  93. Close (f)
  94. End
  95. End Sub
  96.  
  97. Private Sub cmdAdd_Click()
  98. 'rs.AddNew
  99. rs![Name] = txtName.Text
  100. rs![Address1] = txtAddress1.Text
  101. rs![Address2] = txtAddress2.Text
  102. rs![Address3] = txtAddress3.Text
  103. rs![Postcode] = txtPostcode.Text
  104. rs.Update
  105.  
  106. End Sub
Ty for looking.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: vb6/common dialog/access db

 
0
  #2
May 21st, 2005
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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Set db = OpenDatabase(CMDialog1.FileName)

and then this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. OpenFileName = CMDialog1.FileName
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: azkabancells is an unknown quantity at this point 
Solved Threads: 0
azkabancells's Avatar
azkabancells azkabancells is offline Offline
Newbie Poster

Re: vb6/common dialog/access db

 
0
  #3
May 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: vb6/common dialog/access db

 
0
  #4
May 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: azkabancells is an unknown quantity at this point 
Solved Threads: 0
azkabancells's Avatar
azkabancells azkabancells is offline Offline
Newbie Poster

Re: vb6/common dialog/access db

 
0
  #5
May 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: azkabancells is an unknown quantity at this point 
Solved Threads: 0
azkabancells's Avatar
azkabancells azkabancells is offline Offline
Newbie Poster

Re: vb6/common dialog/access db

 
0
  #6
May 22nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: vb6/common dialog/access db

 
0
  #7
May 22nd, 2005
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.
Attached Files
File Type: zip SomeProject.zip (2.6 KB, 30 views)
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: azkabancells is an unknown quantity at this point 
Solved Threads: 0
azkabancells's Avatar
azkabancells azkabancells is offline Offline
Newbie Poster

Re: vb6/common dialog/access db

 
0
  #8
May 22nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: azkabancells is an unknown quantity at this point 
Solved Threads: 0
azkabancells's Avatar
azkabancells azkabancells is offline Offline
Newbie Poster

Re: vb6/common dialog/access db

 
0
  #9
May 22nd, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC