473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

If, IsNull, Then & Else Statements

Hi all,
New to access and need some help. I have a form with two fields on it. TxtStateName and TxtState. I neeed to write an if statement if either one IsNull. Such as:
If IsNull (TxtState) then
msgbox "Need to add State" vbOK
SetFocus back on txt box TxtState
Else continue on ?????

I need this for both txtboxes on the form and has to fire before the before update event, actually it could be wrote in with that code if possible. See Below
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     Dim SN As String
  3.     Dim ST As String
  4.     Dim stLinkCriteria As String
  5.     Dim rsc As DAO.Recordset
  6.     Dim SNkey As Long
  7.     Dim STkey As Long
  8.     Dim TGTkey As Long
  9.     If eMode = True Then
  10.         eMode = False
  11.         Exit Sub
  12.     End If
  13.     Set rsc = Me.RecordsetClone
  14.  
  15.     SN = Me.TxtStateName
  16.     ST = Me.TxtState
  17.     SNkey = Nz(DLookup("StateID", "Tbl_State", "StateName=" & "'" & SN & "'"), 0)
  18.     STkey = Nz(DLookup("StateID", "Tbl_State", "State=" & "'" & ST & "'"), 0)
  19.  
  20.     'Neither exist, add record
  21.     If SNkey = 0 And STkey = 0 Then
  22.         Exit Sub
  23.     End If
  24.  
  25.     'both exist and are in same existing record, get that record
  26.     If SNkey > 0 And SNkey = STkey Then
  27.         TGTkey = SNkey
  28.         Me.Undo
  29.         'Cancel = True
  30.         MsgBox "Warning!  State of " _
  31.         & SN & " and the State Abrev " & ST & " are already in Database." _
  32.         & vbCr & vbCr & "You will now been taken to the record.", _
  33.         vbInformation, "Duplicate Information"
  34.         Call GetRecordToEdit(TGTkey)
  35.         Call CmdEdit_Click
  36.         Exit Sub
  37.     End If
  38.  
  39.     'SN exists but ST does not, get the SN record
  40.     If SNkey > 0 And STkey = 0 Then
  41.         MsgBox StateName & " exists, but " & State & " does not. I will show you the State Name record.", _
  42.         vbInformation + vbOKOnly, "   A T T E N T I O N   "
  43.         Me.Undo
  44.         Call GetRecordToEdit(SNkey)
  45.         Call CmdEdit_Click
  46.         Exit Sub
  47.     End If
  48.  
  49.     'ST exists but SN does not, get the ST record
  50.     If STkey > 0 And SNkey = 0 Then
  51.         MsgBox State & " exists, but " & StateName & " does not. I will show you the State record.", _
  52.         vbInformation + vbOKOnly, "   A T T E N T I O N   "
  53.         Me.Undo
  54.         Call GetRecordToEdit(STkey)
  55.         Call CmdEdit_Click
  56.         Exit Sub
  57.     End If
  58.  
  59.     'both SN and ST exist but not in same record, inform user and get out
  60.     If SNkey > 0 And STkey > 0 And (SNkey <> STkey) Then
  61.         MsgBox Me.State & " and " & Me.StateName & " is an invalid pair.", vbCritical + vbOKOnly, "  I N V A L I D   I N P U T   "
  62.         Me.Undo
  63.         Exit Sub
  64.     End If
  65. End Sub
Dec 6 '18 #1
2 1878
NeoPa
32,556 Expert Mod 16PB
It's good that you've included your code, but expecting people to work out what your question is - or rather should be - by their reading and interpreting your non-working code is not so good.

Why don't you take a step back and put your actual question into clear English so that we have enough understanding of what you need and can help you without wasting inordinate amounts of time trying to guess what that is.

I can see you're a new member, but bear in mind there are threads at the top of the forum that explain what is expected as a minimum when asking for help.
Dec 6 '18 #2
twinnyfo
3,653 Expert Mod 2GB
D9pierce,

Wecome to Bytes!

Another (bot more complicated) approach is to highlight the labels that require entry. Whenever one moves to a different record (using the OnCurrent event), highlight all the required fields. as the user enters data into these fields, if the data is valid, the highlight goes away. If the data entry is not valid, set the focus to the control the user just updated.

But, as NeoPa stated, it's a bit difficult ot understand exactly what your code is trying to do. My first question would be, "Why must the user enter the State and the State Name?" If these items are both from the same table, entering one (or simply selecting one from a combo box) would be sufficient.

The bottom line is that we really need some clarifying information before we can provide more thorough advice.

Thanks.
Dec 6 '18 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: gattaca8 | last post by:
The following script works fine in IE...but gives me a "sBox has no properties" error in Firefox. sBox is only used in the below method. function addTop (sectId) { sBox =...
19
by: GMKS | last post by:
Hello all, I have 13 check boxes on a form. I am trying to check all the check boxes to determine if they are true or false when I close the form. At present only the first IF...Then...Else...
2
by: misscrf | last post by:
I have a search form that is great. I have modified it in such a way, that when search results come up I can bring it back to a useful spot, say an entry form or a report. Here is my lemon (...
2
by: Lee-Anne Waters via AccessMonster.com | last post by:
hi, hopefully an easy one for someone. how do i specify two sets of critera in an if statement for example if isnull(me.txt27) and me.txtfld = "4" then
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
3
by: brynne | last post by:
Can someone give some advice? I am not big on VB scripts, so am trying to write an expression in an update query that is designed to populate a field which I will use as the email first letter. If...
2
by: justbailey | last post by:
I am very new and bad at programming and taking a vb.net class. I am trying to troubleshoot my code to see why it is not running properly. I found a thread in another forum that said I could not...
0
by: Stefanos Efthimiou | last post by:
Hi! I need assistance on the following scenario. I want to have a DB Grid where a user can click on and select from a drop down menu and then see the details of the product automatically filling...
3
by: hrstissiaopai | last post by:
One very important part of almost all computer languages are conditional statements, and specifically if/else statements. Here is an example. if(16==16){ window.alert("Yes, 16 does equal 16!...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.