Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving information from a form

Posted on 2004-04-16
8
Medium Priority
?
307 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
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…
Suggested Courses

783 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