View Full Version : Solved: Error handling
SilverBack
12-04-2005, 05:26 PM
I have a whole bunch of fields on a UserForm and I have to make sure they're all filled in. I have a msgbox that comes up when one or more are not left blank. The problem is when I click the "OK" button the program exits. I'd like for this not to happen.
 
 
If tbKMS <> Empty And tbKFS <> Empty And tbDMS <> Empty And tbDFS <> Empty _
    And tbMnth <> Empty And tbKMB <> Empty And tbKFB <> Empty And tbDMB <> Empty _
    And tbDFB <> emtpy And tbKMD <> emtpy And tbKFD <> Empty And tbAlpha <> Empty _
    And tbBeta <> Empty And tbCharlie <> emtpy And tbZulu <> Empty Then
    
        km = tbKMS.Value
        kf = tbKFS.Value
        dm = tbDMS.Value
        df = tbDFS.Value
        Mnt = tbMnth.Value
        bkm = tbKMB.Value
        bkf = tbKFB.Value
        bdm = tbDMB.Value
        bdf = tbDFB.Value
        mkm = tbKMD.Value
        mkf = tbKFD.Value
        alp = tbAlpha.Value
        beta = tbBeta.Value
        C = tbCharlie.Value
        Z = tbZulu.Value
        
        Else
            
            MsgBox "Enter values in all fields", vbCritical
End If
  
 
Thank ya!
Bob Phillips
12-04-2005, 05:39 PM
What is the OK buttom code doing, and where is that code called from?
SilverBack
12-04-2005, 05:50 PM
Here's the whole subroutine. This one defines the variables and calls another sub.
 
 
Private Sub CommandButton1_Click()
Dim Mnt As Integer 'months
Dim km As Single 'Number of kangaroo males each month
Dim kf As Single 'Number of kangaroo females each month
Dim dm As Single 'Number of dingo males each month
Dim df As Single 'Number of dingo females each month
Dim bkm As Single 'Kangroo male births/month
Dim bkf As Single 'Kangroo female births/month
Dim bdm As Single 'Dingo male births/month
Dim bdf As Single 'Dingo female births/month
Dim mkm As Single 'kangroo male deaths/month
Dim mkf As Single 'kangaroo female deaths/month
Dim md As Single 'dingo deaths/month
Dim alp As Single 'frequency of kangaroos being attacked
Dim beta As Single 'frequency of kangaroos not suriving attack
Dim C As Single 'how effectively dingos consume prey
Dim Z As Single 'how often a dingo dies attempting a attack
 
'Assign values for given variables if all fields are valid
 
If tbKMS <> Empty And tbKFS <> Empty And tbDMS <> Empty And tbDFS <> Empty _
And tbMnth <> Empty And tbKMB <> Empty And tbKFB <> Empty And tbDMB <> Empty _
And tbDFB <> emtpy And tbKMD <> emtpy And tbKFD <> Empty And tbAlpha <> Empty _
And tbBeta <> Empty And tbCharlie <> emtpy And tbZulu <> Empty Then
 
km = tbKMS.Value
kf = tbKFS.Value
dm = tbDMS.Value
df = tbDFS.Value
Mnt = tbMnth.Value
bkm = tbKMB.Value
bkf = tbKFB.Value
bdm = tbDMB.Value
bdf = tbDFB.Value
mkm = tbKMD.Value
mkf = tbKFD.Value
alp = tbAlpha.Value
beta = tbBeta.Value
C = tbCharlie.Value
Z = tbZulu.Value
 
Else
 
MsgBox "Enter values in all fields", vbCritical
 
End If
 
'Call next sub
 
Call KANGAROOPARK(Mnt, km, kf, dm, df, bkm, bkf, bdm, bdf, mkm, mkf, md, alp, beta, C, Z)
End Sub
mdmackillop
12-04-2005, 06:05 PM
It's easier to store the names in an array and check each in turn.  The code exits when the first empty value is found.
 
 Dim MyTB, TB 
MyTB = Array(tbKMS, tbKFS, tbDMS, tbDFS, tbMnth, tbKMB, tbKFB, tbDMB, _
        tbDFB, tbKMD, tbKFD, tbAlpha, tbBeta, tbCharlie, tbZulu)
For Each TB In MyTB
    If TB = "" Then
        MsgBox "Enter values in all fields", vbCritical
        Exit Sub
    End If
Next
        
        km = tbKMS.Value
        kf = tbKFS.Value
        dm = tbDMS.Value
'etc
SilverBack
12-04-2005, 07:52 PM
Thanks that's exactly what I wanted to do!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.