[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1832
  • Last Modified:

Clear form fields after saving

I want my form to clear its field after saving. The code that I have so far is not functioning properly:


Private Sub cmdSav_Click()
 
 
    Dim conCars As ADODB.Connection
    
    Set conCars = Application.CurrentProject.Connection
    
 
    conCars.Execute "INSERT INTO Monitor(SerialNo, ManufacturerSerialNo, Make, " & _
                    "Model, DatePurchased, WarrantyPeriod, MonitorSize, " & _
                    "AssignedOfficer) VALUES('', '', '', '', '', '', '', '')"
 
      
    conCars.Close
    DoCmd.GoToRecord , , acNewRec
    Set conCars = Nothing
End Sub

Open in new window

0
LEBAONEMUN
Asked:
LEBAONEMUN
  • 7
  • 6
  • 2
1 Solution
 
silemoneCommented:
why not pass null as as value and when pulling, use isNull(field, '')
0
 
silemoneCommented:
by the way, more than likely if its not working, it's due to an sql error...try to place in a string first and debug and put a breakpoint at that string and see if maybe it's leaving off an ')' or something...copy and paste result and test directly in access
0
 
peter57rCommented:
The only line of the code shown which could have anything to do with a form is the line:
DoCmd.GoToRecord , , acNewRec

This will only have any effect if you are using a bound form, since there is no 'new record' concept for an unbound form.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LEBAONEMUNAuthor Commented:
I have a 5-tabs form with each tab having a subform that have a table as its source object. Each tab a save button and there is an overall save button. Now I want to make sure each time I press save form fields are cleared. Its not an sql error because it saving fine only that its not clearing fileds.
0
 
peter57rCommented:
If the form/subform is bound the only way you can 'clear' the textboxes is to move to a new record.
For a bound form there must always be a record visible - either an existing saved record or a new record.
0
 
LEBAONEMUNAuthor Commented:
All the subforms are bound. How can I use a for....next loop to achieve this. I have a feeling that can be done through that route..
0
 
peter57rCommented:
I'm not clear what you are asking- are you asking can you loop through a list of subreports moving each one to a new record?
0
 
LEBAONEMUNAuthor Commented:
One way to clear unbound forms is to use a for....next loops. So is it not possible to use this, in this case. I know I said each subform is bound because its 'source object' is pointing to the tables. But again is it possible to loop through each subform moving each one to a new record.
0
 
LEBAONEMUNAuthor Commented:
Ok, I thought that since DoCmd.GoToRecord,,acNewRec wont do it for me I could use the following code. Would it work.
For Each ctl In Me.Controls
    If (ctl.ControlType = acTextBox) Then
        ctl.Value = Null
    End If
Next ctl

Open in new window

0
 
peter57rCommented:
No it will not work.

If you are using a bound form, setting the control value is changing the visible record.

For each of your form you can move to a new record after adding a record by usib=ng the afterinsert event procedure.

You would use the same code in every form:

Private Sub Form_AfterInsert()
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
End Sub
0
 
LEBAONEMUNAuthor Commented:
Thanx for your response. But I cant figure out what Name would be in thi case (Me.Name, acNewRec)
0
 
peter57rCommented:
It is literally Name
0
 
LEBAONEMUNAuthor Commented:
I have put the given code in each form. But when I get to the main form after inserting the records nothing happens still.
0
 
peter57rCommented:
If you have done as I described, as you save each new record in a subform you should move to a new empty record in that subform.
0
 
LEBAONEMUNAuthor Commented:
I chose to intead use the code below for each control in the form and it works fine for me at the moment just have to fine-tune it.
Forms!Computer!TabCtl0.Pages!CPUInfo.Controls!cpu.Controls!Speed.Text = ""

Open in new window

0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now