I am trying to write write a nested if statement in VBA. In cobol, I would typically use the evaluate clause. But what do I use in VBA so as to avoid a long loop.
Example.
if cmbfield = "green" then
me.frame1.enable = true
else
me.frame2.enable = false
me.frame3.enable = false
end if
if cmbfield = "red" then
me.frame2.enable = true
else
me.frame1.enable = false
me.frame3.enable = false
end if
if cmbfield = "white" then
me.frame3.enable = true
else
me.frame1.enable = false
me.frame2.enable = false
end if
In the example you gave I'd use a switch command:
http://www.techonthenet.com/excel/formulas/case.php
Select Case test_expression
Case condition_1
result_1
Case condition_2
result_2
...
Case condition_n
result_n
Case Else
result_else
End Select
You could also do if ... elseif ... end if
http://www.techonthenet.com/excel/formulas/if_then.php
If condition_1 Then
result_1
ElseIf condition_2 Then
result_2
...
ElseIf condition_n Then
result_n
Else
result_else
End If
I do it this way:
Me.frame1.enabled = (cmbfield = "green")
Me.frame2.enabled = (cmbfield = "red")
Me.frame3.enabled = (cmbfield = "white")
Dim isGreen as Boolean, isRed as Boolean, isWhite as Boolean
isGreen = (cmbfield = "green")
isRed = (cmbfield = "red")
isWhite = (cmbfield = "white")
me.frame1.enabled = isGreen
me.frame2.enabled = isRed
me.frame3.enabled = isWhite
This is a shorter way to write the same code. Should work modulo syntax; hope this helps.
Related
I'm trying to update some field value depending two string field using a function with If and Case, function below:
Private Function checkDATI(tipotransazione As String, tipovendita As String) As String
Dim r As String
r = ""
If tipotransazione = "VENDITA" Then
Select Case tipovendita
Case "ARMI"
If Me.txtMatricola = "" Then r = "Matricola"
If Me.txtModello = "" Then r = "Modello"
If Me.txtCalibro = "" Then r = "Calibro"
If Me.txtTipoArma = "" Then r = "Tipo Arma"
If Me.txtFabrica = "" Then r = "Fabbrica"
Case "VENDITA ARMI/MUNIZIONI"
Case "MUNIZIONI"
End Select
End If
If tipotransazione = "ACQUISTO" Then
Select Case tipovendita
Case "ARMI"
If Me.txtMatricola = "" Then r = "Matricola"
If Me.txtModello = "" Then r = "Modello"
If Me.txtCalibro = "" Then r = "Calibro"
If Me.txtTipoArma = "" Then r = "Tipo Arma"
If Me.txtFabrica = "" Then r = "Fabbrica"
Case "VENDITA ARMI/MUNIZIONI"
Case "MUNIZIONI"
End Select
End If
checkDATI = r
End Function
And then when I call this function in a command button event as:
MsgBox (checkDATI(Me.CausaleMov, Me.txt_tipomov))
It's not updating that field. What is wrong?
When you say:
If Me.txtMatricola = "" Then r = "Matricola"
What you are doing is assigning the value: "Matricola" to the variable:r. Instead what I think you want is this:
If Me.txtMatricola = "" Then Me.txtMatricola = "Matricola"
This actually sets the value of the field: txtMatricola to "Matricola". This goes for all of the fields that are in your Case statements.
how to solve this problem in ssis 2005? I'm used script component for validation.
I'm validating each column in script component using VB Script. while import high volume of data more 50 million. If any one column value failed based on my validation script. That time getting following error in ssis (Deadlock detected while trying to lock variables. The locks cannot be acquired after 16 attempts. The locks timed out.)
Public Overrides Sub ValidateColumn_ProcessInputRow(ByVal Row As ValidateColumnBuffer)
Try
Row.IsSkipped = False
'Declare and set variables
Dim Var As IDTSVariables90 = Nothing
Dim str As String
Me.VariableDispenser.GetVariables(Var)
Me.VariableDispenser.LockForRead("User::strEligibilityKeyEmptyMsg")
Me.VariableDispenser.LockForRead("User::strMemberIDEmptyMsg")
Me.VariableDispenser.LockForRead("User::strServiceFromDateEmptyMsg")
Me.VariableDispenser.LockForRead("User::strServiceToDateEmptyMsg")
Me.VariableDispenser.LockForRead("User::strDxCode1EmptyMsg")
Me.VariableDispenser.LockForRead("User::strAllowedAmountEmptyMsg")
Me.VariableDispenser.LockForRead("User::strPaidAmountEmptyMsg")
Me.VariableDispenser.LockForRead("User::strPaidDateEmptyMsg")
Me.VariableDispenser.LockForRead("User::strFacilityIDEmptyMsg")
Me.VariableDispenser.LockForRead("User::strServiceTypeEmptyMsg")
Me.VariableDispenser.LockForRead("User::strServiceFromDateInvalidMsg")
Me.VariableDispenser.LockForRead("User::strPaidDateInvalidMsg")
Me.VariableDispenser.LockForRead("User::strServiceToDateInvalidMsg")
'Field length and Expression validation
Row.RowId = intRowId + 1
intRowId = intRowId + 1
Row.IsValidationSuccess = True
'Required field validation
'User::strEligibilityKeyEmptyMsg - Eligibility Key is empty !
If (Row.EligibilityKey_IsNull Or Row.EligibilityKey.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strEligibilityKeyEmptyMsg").Value.ToString()
Return
End If
'User::strMemberIDEmptyMsg - MemberID is empty !
'If (Row.MemberIDClaimantID_IsNull Or Row.MemberIDClaimantID.Trim().Length = 0) Then
' Row.IsSkipped = True
' Row.ErrorDesc = Var("User::strMemberIDEmptyMsg").Value.ToString()
' Return
'End If
'User::strServiceFromDateEmptyMsg - ServiceFromDate is empty !
If (Row.ServiceFromDate_IsNull Or Row.ServiceFromDate.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strServiceFromDateEmptyMsg").Value.ToString()
Return
End If
'strServiceFromDateEmptyMSg - Invalid ServiceFromDate !
Dim ServiceFromDate1 As Match = Regex.Match(Row.ServiceFromDate, "^([1][012]|[0]?[1-9])[/]([3][01]|[12]\d|[0]?[1-9])[/]([1-8][0-9][0-9][0-9]|9000)$")
'Dim ServiceFromDate1 As Match = Regex.Match(Row.ServiceFromDate, "^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-9][0,9][0-9][0-9]))$")
If Not ServiceFromDate1.Success Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid ServiceFromDate"
Return
End If
'User::strServiceToDateEmptyMsg - ServiceToDate is empty !
If (Row.ServiceToDate_IsNull Or Row.ServiceToDate.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strServiceToDateEmptyMsg").Value.ToString()
Return
End If
'strServiceToDateEmptyMSg - Invalid ServiceToDate !
Dim ServiceToDate1 As Match = Regex.Match(Row.ServiceToDate, "^([1][012]|[0]?[1-9])[/]([3][01]|[12]\d|[0]?[1-9])[/]([1-8][0-9][0-9][0-9]|9000)$")
'Dim ServiceToDate1 As Match = Regex.Match(Row.ServiceToDate, "^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-9][0,9][0-9][0-9]))$")
If Not ServiceToDate1.Success Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid ServiceToDate"
Return
End If
'User::strDxCode1EmptyMsg - DxCode1 is empty !
If (Row.DxCode1_IsNull Or Row.DxCode1.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strDxCode1EmptyMsg").Value.ToString()
Return
End If
'User::strAllowedAmountEmptyMsg - AllowedAmount is empty !
If (Row.AllowedAmount_IsNull) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strAllowedAmountEmptyMsg").Value.ToString()
Return
End If
'Placeofservice value more than 2 digits!
If Row.PlaceofService.ToString().Length > 2 Then
Row.IsSkipped = True
Row.ErrorDesc = "Placeofservice value more than 2 digits!"
Return
End If
'MemberAmount is empty !
If (Row.MemberAmount_IsNull) Then
Row.MemberAmount = 0
End If
'User::strPaidAmountEmptyMsg - PaidAmount is empty !
If (Row.PaidAmount_IsNull) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strPaidAmountEmptyMsg").Value.ToString()
Return
End If
'User::strPaidDateEmptyMsg - PaidDate is empty !
If (Row.PaidDate_IsNull Or Row.PaidDate.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strPaidDateEmptyMsg").Value.ToString()
Return
End If
'strPaidDateEmptyMSg - Invalid PaidDate !
Dim PaidDate1 As Match = Regex.Match(Row.PaidDate, "^([1][012]|[0]?[1-9])[/]([3][01]|[12]\d|[0]?[1-9])[/]([1-8][0-9][0-9][0-9]|9000)$")
'Dim ServiceToDate1 As Match = Regex.Match(Row.ServiceToDate, "^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-9][0,9][0-9][0-9]))$")
If Not PaidDate1.Success Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid PaidDate"
Return
End If
'User::strFacilityIDEmptyMsg - FacilityID is empty !
If (Row.FacilityID_IsNull Or Row.FacilityID.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strFacilityIDEmptyMsg").Value.ToString()
Return
End If
'User::strServiceTypeEmptyMsg- ServiceType is empty !
If (Row.ServiceType_IsNull Or Row.ServiceType.Trim().Length = 0) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strServiceTypeEmptyMsg").Value.ToString()
Return
End If
'Invalid service Type!
If (Row.ServiceType.Trim().ToUpper <> "IPS" And Row.ServiceType.Trim().ToUpper <> "IPA" And Row.ServiceType.Trim().ToUpper <> "OPM" And Row.ServiceType.Trim().ToUpper <> "OPS" And Row.ServiceType.Trim().ToUpper <> "ER" And Row.ServiceType.Trim().ToUpper <> "EM" And Row.ServiceType.Trim().ToUpper <> "ANC") Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid Service Type!"
Return
End If
'In case of any char or special char in integer/money/numeric field, importer must skip those records.
If (Not IsNumeric(Row.AllowedAmount)) Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid char/speceial char is in Allowed Amount"
Return
End If
'In case of any char or special char in integer/money/numeric field, importer must skip those records.
If (Not IsNumeric(Row.PaidAmount)) Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid char/speceial char is in Paid Amount"
Return
End If
'In case of any char or special char in integer/money/numeric field, importer must skip those records.
If (Not IsNumeric(Row.MemberAmount)) Then
Row.IsSkipped = True
Row.ErrorDesc = "Invalid char/speceial char is in Member Amount"
Return
End If
'User::strServiceFromDateInvalidMsg - ServiceFromDate is Invalid Format !
If (Not IsDate(Row.ServiceFromDate)) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strServiceFromDateInvalidMsg").Value.ToString()
Return
End If
'User::strPaidDateInvalidMsg - PaidDate is Invalid Format !
If (Not IsDate(Row.PaidDate)) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strPaidDateInvalidMsg").Value.ToString()
Return
End If
'User::strServiceToDateInvalidMsg - ServiceToDate is Invalid Format !
If (Not IsDate(Row.ServiceToDate)) Then
Row.IsSkipped = True
Row.ErrorDesc = Var("User::strServiceToDateInvalidMsg").Value.ToString()
Return
End If
'PaidDate greater then default date !
If (Convert.ToDateTime(Row.PaidDate.Trim()) > Convert.ToDateTime("12/31/9000")) Then
Row.IsSkipped = True
Row.ErrorDesc = "PaidDate greater than default dates !"
Return
End If
Var.Unlock()
Catch ex As Exception
Row.ErrorDesc = ex.Message().ToString()
End Try
End Sub
Use Var.UnLock() before each Return in the If condition.
Just want to ask why this wont work for me?
I'm trying to bring back data for booking system (or something which like that).
The problem is that it only go to only one if statement and ignore the second one.
Using VB.net and trying to connect to MySQL database.
Thanks all...
Petr
thanks for answers guys i will try all the problem is that if its not = to let say Monday then it should make the color of the check-box white.
I will try the select case and see.
I did some changes add there checkbox lists that it make it easier. The problem is to clear the checkbox on different days.
CheckBoxListMon.BackColor = Drawing.Color.White
CheckBoxListMon.Enabled = True
CheckBoxListMon.ClearSelection()
This make the checkbox enabled and not selected but I still cant can't click on them they are disabled.
Anyone have some idea?
Try
strQuery = "SELECT BookingDate, BookingTime,BookRegUserID,Booked FROM bookings"
MySQLCmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open()
DR = MySQLCmd.ExecuteReader
While DR.Read
bookDate = DR.Item("BookingDate")
bookTime = DR.Item("BookingTime")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")
Select Case True
Case bookDate = lblMonday.Text And CheckBoxListMon.Items.FindByValue(test) IsNot Nothing
CheckBoxListMon.Items.FindByValue(bookTime).Enabled = False
CheckBoxListMon.Items.FindByValue(bookTime).Selected = True
CheckBoxListMon.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
Case bookDate = lblTuesday.Text And CheckBoxListTue.Items.FindByValue(test) IsNot Nothing
CheckBoxListTue.Items.FindByValue(bookTime).Enabled = False
CheckBoxListTue.Items.FindByValue(bookTime).Selected = True
CheckBoxListTue.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
End While
DR.Close()
dbCon.Close()
Catch ex As Exception
End Try
I think Isee the problem now. You dont want to flip the comboboxes back if they do not match. Lets say you have Monday Tuesday being returned in your result set. The first row will flip monday to true and tuesday to false, the second will set monday to false - which I do not think you want - and set tuesday to true, leaving everything being decided by the last row. The answer is to set them all to false before processing the rows, and only set to true if you get a match, otherwise ignore it.
CbMon10.BackColor = Drawing.Color.White
CbMon10.Checked = False
CbMon10.Enabled = True
CbTue10.BackColor = Drawing.Color.White
CbTue10.Checked = False
CbTue10.Enabled = True
While DR.Read
bookDate = DR.Item("BookingDate")
bookTime = DR.Item("BookingTime")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")
Select Case True
Case bookDate = lblMonday.Text And bookTime = CbMon10.Text
CbMon10.BackColor = Drawing.Color.Red
CbMon10.Checked = True
CbMon10.Enabled = False
Case bookDate = lblTuesday.Text And bookTime = CbTue10.Text
CbTue10.BackColor = Drawing.Color.Red
CbTue10.Checked = True
CbTue10.Enabled = False
End Select
End While
You should use if-else-if ladder like this :
If bookDate = lblMonday.Text And bookTime = CbMon10.Text Then
CbMon10.BackColor = Drawing.Color.Red
CbMon10.Checked = True
CbMon10.Enabled = False
Else If bookDate = lblTuesday.Text And bookTime = CbTue10.Text Then
CbTue10.BackColor = Drawing.Color.Red
CbTue10.Checked = True
CbTue10.Enabled = False
Else
CbTue10.BackColor = Drawing.Color.White
CbTue10.Checked = False
CbTue10.Enabled = True
End If
Instead of multiple if's you can use it like this:-
If (bookDate = lblMonday.Text And bookTime = CbMon10.Text) or (bookDate = lblTuesday.Text And bookTime = CbTue10.Text) Then
CbMon10.BackColor = Drawing.Color.Red
CbMon10.Checked = True
CbMon10.Enabled = False
Else
CbMon10.BackColor = Drawing.Color.White
CbMon10.Checked = False
CbMon10.Enabled = True
End If
Hi how can I make this query work. I want a condition on where clause, that if #BACHNUMB = '',
then WHERE is (h.sopnumbe = #SOPNUMBE) Else WHERE is (h.bachnumb = #BACHNUMB). Thanks in advance.
WHERE
CASE(#BACHNUMB)
WHEN '' THEN (h.sopnumbe = #SOPNUMBE)
ELSE
(h.bachnumb = #BACHNUMB)
END
Simply recreate the logic through different syntax:
WHERE
(#BACHNUMB = '' AND h.sopnumbe = #SOPNUMBE)
OR
(#BACHNUMB != '' AND h.bachnumb = #BACHNUMB)
END
(#BACHNUMB = '' and h.sopnumbe = #SOPNUMBE) or (#BACHNUMB != ' and 'h.bachnumb = #BACHNUMB)
I have a VBA conditional script as below:
If COMORBIDITY_CANCER = -1 Or COMORBIDITY_CHRONIC_SKIN_LESIONS = -1 Or COMORBIDITY_CHRONIC_LUNG_DISEASE = -1 Or COMORBIDITY_CARDIOVASCULAR_DISEASE = -1 Or COMORBIDITY_DM = -1 Or COMORBIDITY_IMMUNODEFICIENCY_OR_CHEMOTHERAPY = -1 Or COMORBIDITY_ADL = -1 Or COMORBIDITY_HEPATIC_DISEASE = -1 Or COMORBIDITY_RENAL_IMPAIRMENT = -1 Then
RS_REPORT.AddNew
RS_REPORT!REPORT_UID = REPORT_UID
RS_REPORT!CATEGORY1_ID = 2
RS_REPORT!CATEGORY2_ID = 1
RS_REPORT!CATEGORY3_ID = 1
RS_REPORT.Update
End If
I think the first line of it is too long and I would like to chop it using ENTER, like what can be done in R, but seems it is invalid in VBA, I tried
& _
but it also fails.
Any suggestions? Thanks!
Try this:
If COMORBIDITY_CANCER = -1 Or COMORBIDITY_CHRONIC_SKIN_LESIONS = -1 Or _
COMORBIDITY_CHRONIC_LUNG_DISEASE = -1 Or COMORBIDITY_CARDIOVASCULAR_DISEASE = -1 Or _
COMORBIDITY_DM = -1 Or COMORBIDITY_IMMUNODEFICIENCY_OR_CHEMOTHERAPY = -1 Or _
COMORBIDITY_ADL = -1 Or COMORBIDITY_HEPATIC_DISEASE = -1 Or _
COMORBIDITY_RENAL_IMPAIRMENT = -1 Then
You could try something such as:
Boolean b = false
If condition1 b = true
If condition2 b = true
. . .
If b ...