validation entry (1 Viewer)

mfuada

Registered User.
Local time
Today, 09:24
Joined
Feb 4, 2009
Messages
63
Hey guys.. i'm newbie in access, i just want to ask how to make validation data entry from the form entry using a VBA script.
so what i want to do is when i click the save button, if one of the textbox is empty.. the program will notify the user that the one of the field entry is empty and need to be filled
Here's what i got :

table "info" with fields: no,name,price
and what i got so far on my VBA code is like this:

Private Sub Command15_Click()

Set infodb = CurrentDb().OpenRecordset("info")
If Me!nametxt = "" Then MsgBox "write your name!"
Else
infodb.AddNew
infodb!no = me!infodb_txt
myrec.update

End Sub

and i keep getting message "ELSE without If" could u guys tell me the correct script?
Thx


 

JANR

Registered User.
Local time
Today, 04:24
Joined
Jan 21, 2009
Messages
1,623
You forgot END IF after myrec.update..

JR
 

mfuada

Registered User.
Local time
Today, 09:24
Joined
Feb 4, 2009
Messages
63
I've added the END IF but still getting Message error "Else without If" ...
do i write the validation code wrong or maybe i'm using the IF statement falsely.. could u give me any suggestion or maybe u could give me an example of how to write the correct script...
Thanks...
 

JANR

Registered User.
Local time
Today, 04:24
Joined
Jan 21, 2009
Messages
1,623
Why not use the built-in validation for txtname?

Validationrule -> Is Not Null
Validationtext -> write your name

JR
 

JANR

Registered User.
Local time
Today, 04:24
Joined
Jan 21, 2009
Messages
1,623
Code:
Private Sub Command15_Click()
 
    If IsNull(Me.txtname) Then
        MsgBox "write your name"
        Me.txtname.SetFocus
        Else
        'MsgBox "Run Update"
        'do your update
    End If
End Sub

JR
 

mfuada

Registered User.
Local time
Today, 09:24
Joined
Feb 4, 2009
Messages
63
I've tried your code but it still input the data into the table and the validation message box did not appear....... here's my code i copy it straight from the VBA :).....
could u tell me the faults??

Private Sub Command15_Click()

If IsNull(Me.series_valtxt) Then
MsgBox "write the series!!"
Me.series_valtxt.SetFocus
Else
Set valasdb = CurrentDb().OpenRecordset("valas_coupon")
valasdb.AddNew
valasdb!series_val = Me!series_valtxt
valasdb.Update
End If
End Sub
 

JANR

Registered User.
Local time
Today, 04:24
Joined
Jan 21, 2009
Messages
1,623
I have no clue because it works on my test.

But this line: Set valasdb = CurrentDb().OpenRecordset("valas_coupon")

Don't you get an error on this without a Declaration on valasdb??
I would put an OPTION EXPLICIT on top on your module to trap undeclared vaiables.

JR
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 03:24
Joined
Sep 12, 2006
Messages
15,709
why do you need all this?

i can't see why you need to open a recordset to validate an entry

-----------
just use a bound form

set certain fields as required = true

access will do it all for you

all you might need to do is intercept some access warnings/errors and replace them with nicer ones of your own - but if you are just trying to get started with access, you are going about it the wrong way - what you are trying to do looks really complex and unnecessary
 

mfuada

Registered User.
Local time
Today, 09:24
Joined
Feb 4, 2009
Messages
63
hi..
my first plan with the code is when i click the "save" button then it will check the empty textbox first, and then if all field is filled the code it will save the data in the textbox to the table... that's why i use the IF statement first to check the empty textbox.. and then use open recordset and so on, to save the data to the table...
do i get it all wrong in access, could you give me suggestion of how it's all be done?
and could you be more specific with your post earlier? cos i can't find where to set the certain field to "true"
Thanks
 

rapsr59

Registered User.
Local time
Yesterday, 20:24
Joined
Dec 5, 2007
Messages
93
Try writing the original code:

Code:
    Set infodb = CurrentDb().OpenRecordset("info")
    If Me!nametxt = "" Then
        MsgBox "write your name!"
    Else
        infodb.AddNew
        infodb!no = Me!infodb_txt
        myrec.Update
    End If


All I did was change:

Code:
If Me!nametxt = "" Then MsgBox "write your name!"



to:

Code:
   If Me!nametxt = "" Then
        MsgBox "write your name!"

And Added the:

Code:
    End If


Also: I think you might want to change "myrec.Update" in your code to "infodb.Update" and I noticed you did not dimension your recordset infodb. Your code would be:

DIM infodb As DAO.Recordset

This DIM statement would be entered before:

Set infodb = CurrentDb().OpenRecordset("info")


The the code would look like this:

Code:
    Dim infodb As DAO.Recordset
 
    Set infodb = CurrentDb().OpenRecordset("info")
    If Me!nametxt = "" Then 
        MsgBox "write your name!"
    Else
        infodb.AddNew
        infodb!no = Me!infodb_txt
        infodb.Update
 
    End If

I hope this helps


Richard
 
Last edited:

mfuada

Registered User.
Local time
Today, 09:24
Joined
Feb 4, 2009
Messages
63
wow thx alot.. i'll try it for sure.... thx again!!:)
 

rapsr59

Registered User.
Local time
Yesterday, 20:24
Joined
Dec 5, 2007
Messages
93
As far as your orignal error: "Else without If"...

As JANR posted:

Code:
Private Sub Command15_Click()
 
    If IsNull(Me.txtname) Then
        MsgBox "write your name"
        Me.txtname.SetFocus
        Else
        'MsgBox "Run Update"
        'do your update
    End If
End Sub

And back to your original code, you wrote:

Code:
Private Sub Command15_Click()
 
    If IsNull(Me.txtname) Then MsgBox "write your name"
    'etc...
 
End Sub

Notice that the MsgBox statment in your code is on the same line as the IF statment and JANR's MsgBox statement is written on the second line.

You can write the IF statment and write what is to take place on the same line when only one condition is to be tested.

JANR and I both wrote the MsgBox statement on the following line because your code is testing multiple conditions.

This is why you were getting the "Else without If" error.

Hope this makes understanding the difference in the code more transparent.

Richard
 
Last edited:

rapsr59

Registered User.
Local time
Yesterday, 20:24
Joined
Dec 5, 2007
Messages
93
The very last thing!

as JANR posted:

But this line: Set valasdb = CurrentDb().OpenRecordset("valas_coupon")

Don't you get an error on this without a Declaration on valasdb??
I would put an OPTION EXPLICIT on top on your module to trap undeclared vaiables.


JANR and I suggest you use OPTION EXPLICIT in all of your modules to trap undeclared variables.

You can have the VBA editor automatically place the OPTION EXPLICIT in all of your modules by...

opening the vba editor, click Tools, select options and on the "Editor" tab select "Require Variable Declaration" so it is check marked. Then all your modules with have the OPTION EXPLICIT declared in the Declaration section.

Then when you compile your code, VBA will check that all variables have been declared.

You will find that, by taking this step, you will avoid a lot of frustration when debugging your code.


Richard
 

mfuada

Registered User.
Local time
Today, 09:24
Joined
Feb 4, 2009
Messages
63
Thx a lot Richard.. i have tried the code on the different line and it works.. and also i have marked Require Variable Declaration on my VBA editor...
Thanks for sharing this with me.... :)
 

Users who are viewing this thread

Top Bottom