error in code (1 Viewer)

mohamedmatter

Registered User.
Local time
Yesterday, 21:30
Joined
Oct 25, 2015
Messages
112
i want to correct vba code in form under save command
when open form and click save massage display request write first name and last name and when i go to new record and click save command not request any thing and show massage save successful but no any record save in table
thanks for you
i attach sample workers db
 

Attachments

  • Workers.accdb
    1.2 MB · Views: 60

Gasman

Enthusiastic Amateur
Local time
Today, 05:30
Joined
Sep 21, 2011
Messages
14,231
For the benefit of others, this is the code
Code:
Private Sub Command103_Click()
    If IsNull(Me.txtFrist) Then
        MsgBox "Please Write FirstName", vbInformation, "Warning"
        FirstName = Me.txtFrist
        Exit Sub
    End If
    If IsNull(Me.txtLast) Then
        MsgBox "Please Write LastName", vbInformation, "Warning"


        Exit Sub
    End If
    LastName = Me.txtLast
    MsgBox "Saved successful", vbInformation, "Saved"
    DoCmd.GoToRecord , , acNewRec
    Me.txtFrist = ""
    Me.txtLast = ""

End Sub
Why are you setting FirstName if the control is NULL?
Why are you setting them, when the form is bound to the table and those controls to those fields?
Testing for most controls would be in the form's BeforeUpdate event.
 

vba_php

Forum Troll
Local time
Yesterday, 23:30
Joined
Oct 6, 2019
Messages
2,880
behind your save button, put:


Code:
Private Sub Command103_Click()
If IsNull(Me.FirstName) Then
        MsgBox "Please Write FirstName", vbInformation, "Warning"
        Exit Sub
End If
If IsNull(Me.LastName) Then
        MsgBox "Please Write LastName", vbInformation, "Warning"
        Exit Sub
         End If

            DoCmd.Save
           MsgBox "Saved successful", vbInformation, "Saved"
        DoCmd.GoToRecord , , acNewRec
        Me.FirstName = ""
        Me.LastName = ""

End Sub
on a side note, you've got 4 textboxes on your form. u only need 2. my code looks at the other textboxes that your code does not. they are closer to your "firstname" and "lastname" labels.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 05:30
Joined
Sep 21, 2011
Messages
14,231
Are you wanting to do this bound or unbound as you have both?

Might be better to give us what you are using, rather than the dummy form that appears to be a bound and unbound form for the same record source.

Not something I have come across before?
 

mohamedmatter

Registered User.
Local time
Yesterday, 21:30
Joined
Oct 25, 2015
Messages
112
Why are you setting First Name if the control is NULL?
this is unbound first name field equal first name in table
Why are you setting last Name if the control is NULL?
this is unbound last name field equal last name in table
end
vba by vba_php not work
I want when go to new record and when i click save button request write first name and last name
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 05:30
Joined
Sep 21, 2011
Messages
14,231
My understanding is that if you use unbound controls, you update/append with a query.?
If bound controls, then you save as a matter of course and can force a save.

I tend to use bound controls, just for ease of use, so I will bow out.
 

mohamedmatter

Registered User.
Local time
Yesterday, 21:30
Joined
Oct 25, 2015
Messages
112
I want to work inside unbound objects to make sure there is no error inside the table and I will delete the bound fields from the form after completing the code
 

vba_php

Forum Troll
Local time
Yesterday, 23:30
Joined
Oct 6, 2019
Messages
2,880
try this..........
 

Attachments

  • Workers - working.accdb
    1.2 MB · Views: 57

Users who are viewing this thread

Top Bottom