473,396 Members | 1,713 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,396 software developers and data experts.

How to include variables in msgBox??

70 64KB
I have a small if statement I built here:

Expand|Select|Wrap|Line Numbers
  1.         If Nz(Me.cboEquipmentStatus, "") = "" _
  2.         Or Nz(Me.cboEquipmentModelNum, "") = "" _
  3.         Or Nz(Me.txtSerialNum, "") = "" Then
  4.             MsgBox "Equipment Status, Model Number, and Serial Number cannot be empty!", vbOKOnly
  5.                 If Nz(Me.cboEquipmentStatus, "") = "" Then
  6.                     Me.cboEquipmentStatus.SetFocus
  7.                 ElseIf Nz(Me.cboEquipmentModelNum, "") = "" Then
  8.                     Me.cboEquipmentModelNum.SetFocus
  9.                 ElseIf Nz(Me.txtSerialNum, "") = "" Then
  10.                     Me.txtSerialNum.SetFocus
  11.                 End If
  12.                     Exit Sub
  13.         Else
  14.             If Me.cboEquipmentStatus.Value = 2 Then
  15.                 addStorage = True
  16.             ElseIf Me.cboEquipmentStatus.Value = 1 Then
  17.                 addStorage = False
  18.             End If
  19.         End If
  20.  
Basically it assigns a true/false value to a boolean variable based on the value of a combo box. If one of the 3 required boxes are null, it tells the user that the boxes cannot be null and then it sets focus to the box which is null.

Taking this a step further and also because I can use this knowledge in other areas of my database, I would like to know how to be able to tell the user "Equipment Status cannot be empty!" Or, "Model Number cannot be empty!" etc. etc.

...How is this done?
Nov 20 '15 #1

✓ answered by jforbes

You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. Dim sValidationText As String
  2.  
  3. ' Build Validation Text
  4. If Nz(Me.cboEquipmentStatus, "") = "" Then
  5.     If Len(sValidationText) = 0 Then Me.cboEquipmentStatus.SetFocus
  6.     sValidationText = sValidationText  & "Equipment Status needs to be selected." & vbCrLF
  7. End If
  8. If Nz(Me.cboEquipmentModelNum, "") = "" Then
  9.     If Len(sValidationText) = 0 Then Me.cboEquipmentModelNum.SetFocus
  10.     sValidationText = sValidationText  & "Equipment Model needs to be filled out." & vbCrLF
  11. End If
  12. If Nz(Me.txtSerialNum, "") = "" Then
  13.     If Len(sValidationText) = 0 Then Me.txtSerialNum.SetFocus
  14.     sValidationText = sValidationText  & "Serial Number needs to be filled out." & vbCrLF
  15. End If
  16.  
  17. If Len(sValidationText) > 0 Then
  18.    Call MsgBox(sValidationText)
  19. Else
  20.     addStorage = (Me.cboEquipmentStatus.Value = 2)
  21. End If

5 1620
ittechguy
70 64KB
Thinking about this some more, I could use multiple msgboxes.

Like
Expand|Select|Wrap|Line Numbers
  1. if isnull(cboEquipmentStatus) Then
  2. MsgBox "equipment status can't be blank."
  3. ElseIf ... modelnum.. then
  4. MsgBox "model number can't be blank."
The problem with that besides creating more code is that if 2-3 of the controls are blank then there would be 2-3 msgboxes. I only want one.

I.e. if equipment status and modelnum was blank it'd say "equipment status and model num is blank..."
Nov 20 '15 #2
jforbes
1,107 Expert 1GB
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. Dim sValidationText As String
  2.  
  3. ' Build Validation Text
  4. If Nz(Me.cboEquipmentStatus, "") = "" Then
  5.     If Len(sValidationText) = 0 Then Me.cboEquipmentStatus.SetFocus
  6.     sValidationText = sValidationText  & "Equipment Status needs to be selected." & vbCrLF
  7. End If
  8. If Nz(Me.cboEquipmentModelNum, "") = "" Then
  9.     If Len(sValidationText) = 0 Then Me.cboEquipmentModelNum.SetFocus
  10.     sValidationText = sValidationText  & "Equipment Model needs to be filled out." & vbCrLF
  11. End If
  12. If Nz(Me.txtSerialNum, "") = "" Then
  13.     If Len(sValidationText) = 0 Then Me.txtSerialNum.SetFocus
  14.     sValidationText = sValidationText  & "Serial Number needs to be filled out." & vbCrLF
  15. End If
  16.  
  17. If Len(sValidationText) > 0 Then
  18.    Call MsgBox(sValidationText)
  19. Else
  20.     addStorage = (Me.cboEquipmentStatus.Value = 2)
  21. End If
Nov 20 '15 #3
zmbd
5,501 Expert Mod 4TB
Here's a generic code block I use for unbound controls and forms. In bound forms I will set, at the table level, the field required property to true and let Access do the heavy lifting.

+ This makes use of the Tag property of the control by setting the value to the text you want displayed in the message.

+ There is an assumption here that only data-entry controls such as textbox, combobox, listbox, option buttons/groups, will have this information set.
++ one glitch is with the option button... it's always in the null state if unbound when the form loads. Unfortunately, this appears as a non-selected option which can confuse the user depending on use. I typically use this to force confirmation of action... ie, must select the option button allow the delete/save and then there's code to verify the state of the option control - I'll tend to use the toggle button in triple state in these cases as it's clearer to the end user that some action need be taken (I set the caption here to for user feed back :-) )

+ I have a modified version that handles control tags with multiple entries as I often use this property for other tasks. This is a modification of the hidden label method described bellow.
[{required(0/1),{CtrlName4Msg/0},{other flags}]
Check Left(1) if not zero then parse the tag, Once again the Split() comes in handy here.

+ I typically use this method in a command button; however, I have had success using it in the form's before_update event. It really depends on the form's design.

+ I have also used a version where I use a hidden label control (typically I place this in the form footer) that has a comma-delimited list for the caption [{ControlName},{UserText}...] that I pull into an array using the Split() and step thru. I have however, abandoned this method and as I find it in my older databases have replaced it with on of my tag property versions as several people have deleted this control off of forms by accident when adding/changing the design while I was on vacation - arrrghhhh.

+ You will see the use of the tabindex property here, the focus will be set to the first control in the tab order that has a missing entry. SO, take a few moments with the tabstop tool in the form design to logically order your data entry controls! Often the default is correct; however, adding and subtracting controls during design can really muck this order.

+ I've avoided NZ() here because I have had users go back and clear their entry; thus, the control is no-longer null.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2.     Dim zCtrl As Control
  3.     Dim zMsg As String
  4.     Dim zTabIndex As Long
  5.     Dim zCtrlname As String
  6.     '
  7.     'seed the message:
  8.     zMsg = "The following controls are required entry: " & vbCrLf
  9.     '
  10.     'initialze the placeholder.
  11.     zTabIndex = Me.Controls.Count
  12.     For Each zCtrl In Me.Controls
  13.         With zCtrl
  14.             If Not "" & .Tag = "" Then
  15. '            Debug.Print .Name, .Tag, .TabIndex, .Value
  16.                 If "" & zCtrl.Value = "" Then
  17.                     zMsg = zMsg & .Tag & vbCrLf
  18.                     If .TabIndex <= zTabIndex Then
  19.                         zTabIndex = .TabIndex
  20.                         zCtrlname = .Name
  21.                     End If
  22.                 End If
  23.             End If
  24.         End With
  25.     Next
  26.     Debug.Print "setfocusto: " & zCtrlname
  27.     If Not "" & zCtrlname = "" Then
  28.         Me.Controls(zCtrlname).SetFocus
  29.         MsgBox prompt:=zMsg, Buttons:=vbOKOnly, Title:="Missing data in required fields"
  30.     End If
  31. zcleanup:
  32. Exit Sub
  33. zerrtrap:
  34.     MsgBox prompt:="Oh Bother, Contact the DBA and report the following information" & _
  35.         vbCrLf & "errS: " & Err.Source & _
  36.         vbCrLf & "errN: " & Err.Number & _
  37.         vbCrLf & "errD: " & Err.Description, _
  38.         Title:="Requred Dataentry Validation Failure"
  39.     Resume zcleanup
  40. End Sub
I've used this version dozens of times with a 90% success rate... typically the error was due to my mistake (accidentally setting the tag in a label :) ); however, I have occasionally had weird things occur that went away after the database was closed and re-opened.
Nov 20 '15 #4
NeoPa
32,556 Expert Mod 16PB
I expect ZMBD's solution is a good one but my typical approach is similar to JForbes' one.

In your procedure you start with a validation string which is empty. Then you check each item in turn.

If the item is blank (TextBox controls are generally Null when empty rather than empty strings.) then :
  1. If the validation string is empty then this is the first problem so you set the focus to this control.
  2. Regardless of which is first you then add a reference to the control or field into the string in such a way as to form a list if there are multiple problems.
When all the items have been checked, if the validation string is empty then skip anything else.
Otherwise you can add the list between other strings to make up a message that makes sense and handles all of your problems and display that message in your (single) MsgBox().
Nov 20 '15 #5
ittechguy
70 64KB
Thanks for all the help guys! jforbes' method worked perfectly. I'll be using it for future forms as well.
Nov 20 '15 #6

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

Similar topics

23
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
6
by: Henk ten Bos | last post by:
Hi all, is there anyone who knows of a way in VB6 to have include-files (like in C: include <include.h>) ? thanks! Henk
6
by: chris | last post by:
im pretty ne to java script - so please be gentle :) basically what i want to do is construct an action in a function using a variable sent to that function ok - what i want to do is ...
5
by: Tom Petersen | last post by:
I have this: 1 emailBody = "<html>" & vbCrLf _ 2 & "<head>" & vbCrLf _ 3 & "<title>Interpreter Request details</title> " & vbCrLf _ 4 ...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: J. Ptak | last post by:
Hey Tibby. The proper way to create a session variable is: Session = value; Or you can use the Add method as: Session.Add("VarName", value);
7
by: wandoledzep | last post by:
I'm using Visual Studio .NET 2002 I remember there was a way to do this in C, so there should be a way of doing it in .NET: Instead of: Dim bolSquare1 as Boolean Dim bolSquare2 as Boolean...
5
by: Terry On Windigo | last post by:
I think I have figured out my problem but I don't know how to solve it. We are going to start using a forums package and do not want our users to have to login to both our site, and then again to...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
6
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.