Solved

Saving information from a form

Posted on 2004-04-16
8
294 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
 

Author Comment

by:illion
ID: 10844077
Well the Text1.Text works if i use Text1.setFocus before.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now