Solved

Saving information from a form

Posted on 2004-04-16
8
297 Views
Last Modified: 2011-09-20
I have a main menu where you can choose to put in a new Party. This then opens a form named "Party", when i have entered all the information i have a button that says "Save Party", how do i save all the information?
How do i get the information?
Forms!Party.fields?

And if it exists it should prompt a box asking about overwriting, else just save all the information and then close the form and bring up a new empty "Party" form.

Im working in Mircosoft Access 2003, having a database and im inputting alot of information every day, im building a program for reservations.
0
Comment
Question by:illion
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10843803
You could create a folder called PartyFiles in your apps directory and save each party to a file based on filename.For example:

' in declarations area at top of form
Dim PartyFolder As String, PartyFile As String

Private Sub Form_Load()
' make sure apps path is not a root dir
If Right$(App.Path, 1) <> "\" Then
    PartyFolder = App.Path & "\" & "PartyFiles"
Else
    PartyFolder = App.Path & "PartyFiles"
End If
' create folder for new partyfiles
If Dir$(PartyFolder, vbDirectory) = "" Then
    MkDir PartyFolder
End If
End Sub

' Save Party
Private Sub Command1_Click()
Dim ff As Integer
PartyFile = PartyFolder & Text1.Text ' where Text1 is the name of the party

' save to file
ff = FreeFile
Open PartyFile For Output As #ff
Print #ff, Text1.Text
Print #ff, Text2.Text
Print #ff, Text3.Text
Close #ff

End Sub

' retrieve party
Private Sub Command2_Click()
Dim ff As Integer, Ln As String
PartyFile = PartyFolder & Text1.Text ' where Text1 is the name of the party

' retrieve data fom file
ff = FreeFile
' open file for reading
Open PartyFile For Input As #ff
' read line 1 and put into Text1
Line Input #ff, Ln
Text1.Text = Ln
' read Line 2 and put into Text2
Line Input #ff, Ln
Text2.Text = Ln
' read Line 3 and put into Text3
Line Input #ff, Ln
Text3.Text = Ln
' close file
Close #ff

End Sub
0
 

Author Comment

by:illion
ID: 10844003
OK, but  i use a database called BB.mdb that stores all the partys in a Table called "Party".
I have the fields:

1 Party Name
2 Adults
3 Childs
4 Comments
0
 

Author Comment

by:illion
ID: 10844034
I have tried to use Text1.Text but it doesnt work, i have to use Text1.Value else i will get an error, and by the way how do i write when using "Party Name" as a name of the field?
"Party Name".Text?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:illion
ID: 10844077
Well the Text1.Text works if i use Text1.setFocus before.
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10844132
Did your question state Microsoft access at first or did you add that?Im just wondering because I don't remember seeing it when I first read your question.

Im sure there is an easier way to do it from access,but in VB6 you could use something like this:

Private Sub Command1_Click()
Dim DataBase As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
DataBase = "C:\BB.mdb"

' open Database Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & DataBase & "'"
cn.CursorLocation = adUseClient
cn.Open

' open recordset
Set rs = New ADODB.Recordset
rs.Open "Select * From Party", cn, adOpenKeyset, adLockOptimistic

' add new record to Table
rs.AddNew
rs.Fields("Party Name").Value = Text1.Text
rs.Fields("Adults").Value = Text2.Text
rs.Fields("Childs").Value = Text3.Text
rs.Fields("Comments").Value = Text4.Text
rs.Update

' close recordset
rs.Close
Set rs = Nothing

' close connection
cn.Close
Set cn = Nothing
End Sub

You could try putting square brackets around fields names with a space:

rs![Party Name]

0
 

Author Comment

by:illion
ID: 10844221
' open recordset
Set rs = New ADODB.Recordset
rs.Open "Select * From Party Where [Party Name] = '" & Text1.Text & "'", cn, adOpenKeyset, adLockOptimistic

' add new record to Table
if rs.eof then
rs.AddNew
end if
rs.Fields("Party Name").Value = Text1.Text
rs.Fields("Adults").Value = Text2.Text
rs.Fields("Childs").Value = Text3.Text
rs.Fields("Comments").Value = Text4.Text
rs.Update

is this possible if record exists?
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 75 total points
ID: 10844276
You could try checking the recordcount.If it is 0,then addnew otherwise just update the current record.

Set rs = New ADODB.Recordset
rs.Open "Select * From Party Where [Party Name]='" & Text1.Text & "'", cn, adOpenKeyset, adLockOptimistic

If rs.Recordcount = 0 Then
    rs.AddNew
End If

rs.Fields("Party Name").Value = Text1.Text
rs.Fields("Adults").Value = Text2.Text
rs.Fields("Childs").Value = Text3.Text
rs.Fields("Comments").Value = Text4.Text
rs.Update
0
 

Author Comment

by:illion
ID: 10844336
Thanks for your help, now i can get starting building this thing, 25 extra for the extra effort.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 65
Access query that references subform 5 47
How to make an ADE file by code? 11 94
How to read File Date Created using VB6 8 52
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question