Forms not able to insert new record (1 Viewer)

newbie1

New member
Local time
Today, 10:37
Joined
Oct 10, 2019
Messages
6
Hi there,
It has been a while since I did programming and touching MS Access.

So recently I created a simple MS Access Form, everything is working well until I added unbound combo fields to the form and it stopped working. Since the addition of unbound combo fields, the insert fails to work.

I tested by removing the unbound combo fields, the insert works again...

so not too sure what is causing this problem?

:banghead:
 

June7

AWF VIP
Local time
Today, 09:37
Joined
Mar 9, 2014
Messages
5,470
Presume you meant combobox controls, not fields. Unbound controls should not affect adding records.

If you want to provide db for analysis, follow instructions at bottom of my post.
 

newbie1

New member
Local time
Today, 10:37
Joined
Oct 10, 2019
Messages
6
ok, uploaded db here.
 

Attachments

  • test.zip
    59.2 KB · Views: 55

June7

AWF VIP
Local time
Today, 09:37
Joined
Mar 9, 2014
Messages
5,470
Multiple similar name fields indicates a non-normalized data structure - more like a spreadsheet. And now I see you are exporting data to Excel. Are you saying the export fails? Would have been helpful to provide workbook as well. However, I created a workbook with the name used in code and no problem running. Data is exported.

Those Cancel = True lines are meaningless in button click event.

You have more required fields than code validates and record cannot be saved until all are filled in. Because of error handler code, users don't get prompted for missing data. Shouldn't you make sure record is committed before exporting to Excel?
 
Last edited:

missinglinq

AWF VIP
Local time
Today, 13:37
Joined
Jun 20, 2003
Messages
6,423
When validating multiple fields, you have to Exit the Sub each time a validation fails, giving the user a chance to correct errant Control, before moving on to the next validation check...otherwise the code simply 'drops thru' to the next check!

Also, your Comboboxs are all Bound (have Control Sources) and when used like this, to populate Fields in the Form's underlying Table, should be Unbound. Delete the Control Sources for each cbo.

Now, try replacing the validation portion of your code like this...and see what happens (copy your current code and save it somewhere safe!!!)

Code:
Private Sub cmdSaveRecord_Click()


      
 If Len(Me.Customer & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter company name."
 
      Me.Customer.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
 
 ElseIf Len(Me.Duration & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter duration."
 
      Me.Duration.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]

 ElseIf Len(Me.ContactPerson & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter contact person name."
 
      Me.ContactPerson.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
 
 ElseIf Len(Me.ContactNo & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter contact number."
 
      Me.ContactNo.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
      
 ElseIf Len(Me.Email & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter email address."
 
      Me.Email.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]

ElseIf Len(Me.Item1 & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter item code."
 
      Me.Item1.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
      
 ElseIf Len(Me.Desc1 & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter description."
 
      Me.Desc1.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
      
 ElseIf Len(Me.Rate1 & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter rates."
 
      Me.Rate1.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]

Else

[COLOR="Red"][B]DoCmd.RunCommand acCmdSaveRecord[/B][/COLOR]

..............

Notice that I've also saved the Record, as June7 suggested, and I'm not really sure what you're doing with the error handling you're attempting. You're using an Error Handler that is only set after all of the Sub has run (handlers should be set directly under the Sub's name) and you're using both an EH and On Error Resume Next. I'd comment out all of this code, for now.

Also, note that the user will have to click on the 'Save' button again, after correcting each errant Control.

Linq ;0)>
 

June7

AWF VIP
Local time
Today, 09:37
Joined
Mar 9, 2014
Messages
5,470
Wait, why should all comboboxes be UNBOUND? Only Category boxes need to be UNBOUND. Comboboxes are working fine.

Also, code worked without all those additional Exit Sub. When an If condition is met, that segment runs then execution drops out to End If. It's placement of GoToRecord and funky error handling that is issue.
 
Last edited:

newbie1

New member
Local time
Today, 10:37
Joined
Oct 10, 2019
Messages
6
Wait, why should all comboboxes be UNBOUND? Only Category boxes need to be UNBOUND. Comboboxes are working fine.

Also, code worked without all those additional Exit Sub. When an If condition is met, that segment runs then execution drops out to End If. It's placement of GoToRecord and funky error handling that is issue.


Hi June7,

Thanks for your help.

so is this line that is the problem? It was working fine previously though...

Code:
Private Sub cmdSaveRecord_Click()
.

.

.

On Error GoTo NewRecord_Err
    
    On Error Resume Next
    DoCmd.GoToRecord , "", acNewRec
    
NewRecord_Exit:
    Exit Sub
    
NewRecord_Err:
    Beep
    MsgBox Error$
    Resume NewRecord_Exit
    
    Exit Sub
    
errHandler:
    
    MsgBox Err.Number & ": " & Err.Description

End Sub
 

newbie1

New member
Local time
Today, 10:37
Joined
Oct 10, 2019
Messages
6
When validating multiple fields, you have to Exit the Sub each time a validation fails, giving the user a chance to correct errant Control, before moving on to the next validation check...otherwise the code simply 'drops thru' to the next check!

Also, your Comboboxs are all Bound (have Control Sources) and when used like this, to populate Fields in the Form's underlying Table, should be Unbound. Delete the Control Sources for each cbo.

Now, try replacing the validation portion of your code like this...and see what happens (copy your current code and save it somewhere safe!!!)

Code:
Private Sub cmdSaveRecord_Click()


      
 If Len(Me.Customer & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter company name."
 
      Me.Customer.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
 
 ElseIf Len(Me.Duration & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter duration."
 
      Me.Duration.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]

 ElseIf Len(Me.ContactPerson & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter contact person name."
 
      Me.ContactPerson.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
 
 ElseIf Len(Me.ContactNo & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter contact number."
 
      Me.ContactNo.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
      
 ElseIf Len(Me.Email & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter email address."
 
      Me.Email.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]

ElseIf Len(Me.Item1 & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter item code."
 
      Me.Item1.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
      
 ElseIf Len(Me.Desc1 & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter description."
 
      Me.Desc1.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]
      
 ElseIf Len(Me.Rate1 & vbNullString) = 0 Then
 
      ' Alert the user.
      MsgBox "Please enter rates."
 
      Me.Rate1.SetFocus
      [COLOR="Red"]Exit Sub[/COLOR]

Else

[COLOR="Red"][B]DoCmd.RunCommand acCmdSaveRecord[/B][/COLOR]

..............

Notice that I've also saved the Record, as June7 suggested, and I'm not really sure what you're doing with the error handling you're attempting. You're using an Error Handler that is only set after all of the Sub has run (handlers should be set directly under the Sub's name) and you're using both an EH and On Error Resume Next. I'd comment out all of this code, for now.

Also, note that the user will have to click on the 'Save' button again, after correcting each errant Control.

Linq ;0)>

Hi missinglinq,

Thanks for your help... I need all these comboboxes (except Category) to be bound as it will be data entry into the table... Except for category which is unbound as a selection to populate into the description field.

I will try out the error handling portion.
 

newbie1

New member
Local time
Today, 10:37
Joined
Oct 10, 2019
Messages
6
Multiple similar name fields indicates a non-normalized data structure - more like a spreadsheet. And now I see you are exporting data to Excel. Are you saying the export fails? Would have been helpful to provide workbook as well. However, I created a workbook with the name used in code and no problem running. Data is exported.

Those Cancel = True lines are meaningless in button click event.

You have more required fields than code validates and record cannot be saved until all are filled in. Because of error handler code, users don't get prompted for missing data. Shouldn't you make sure record is committed before exporting to Excel?

Hi June7,

Yup the export to excel works just that the data entry to table fails.

I will remove those cancel=true.

I will go through those required fields (is under the form properties for that field right)?
 

June7

AWF VIP
Local time
Today, 09:37
Joined
Mar 9, 2014
Messages
5,470
The fields are set as required in table. This will prevent saving record until those bound controls have data.

I have no problem with your original code as long as I fill in every control (except for all the Item and Category controls, only 1 set is required).

Still suggest should commit record before export to Excel.
 
Last edited:

newbie1

New member
Local time
Today, 10:37
Joined
Oct 10, 2019
Messages
6
The fields are set as required in table. This will prevent saving record until those bound controls have data.

yup i just make this quick change, silly me cos earlier i remove some validation but forgotten about the table validation.

now its working again!

thank you so much
 

Users who are viewing this thread

Top Bottom