Link to home
Start Free TrialLog in
Avatar of illion
illion

asked on

Saving information from a form

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.
Avatar of vinnyd79
vinnyd79

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
Avatar of illion

ASKER

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
Avatar of illion

ASKER

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?
Avatar of illion

ASKER

Well the Text1.Text works if i use Text1.setFocus before.
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]

Avatar of illion

ASKER

' 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?
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of illion

ASKER

Thanks for your help, now i can get starting building this thing, 25 extra for the extra effort.