At present, I have a functioning Select Case that states that if a textbox is blank then it is to be highlighted red, but it only seems to be highlighting one of the textboxes. For instance, if 2 textboxes are left blank, it only highlights the first on it comes across.
Select Case True
Case Me.CustName = ""
Me.CustName.BackColor = &H8080FF
Case Me.RegAddress = ""
Me.RegAddress.BackColor = &H8080FF
Case Me.PostInput = ""
Me.PostInput.BackColor = &H8080FF
Case Me.Landline = ""
Me.Landline.BackColor = &H8080FF
Case Me.Contact = ""
Me.Contact.BackColor = &H8080FF
Case Me.DOBInput = ""
Me.DOBInput.BackColor = &H8080FF
End Select
Being new to VBA, my only thinking is to create a loop round my current code that state (loop until x,y or z is <> "") but I can't seem to figure out how to do this.
Any advice will be greatly appreciated.
Select Case runs the code block following the first matching Case statement only. If you need to check each of your conditions regardless, you should write them as individual If statements instead:
If Me.CustName = "" Then Me.CustName.BackColor = &H8080FF
If Me.RegAddress = "" Then Me.RegAddress.BackColor = &H8080FF
If Me.PostInput = "" Then Me.PostInput.BackColor = &H8080FF
....
You are using Select Case for the wrong purpose. Its purpose is to test a single expression and execute one branch based on the value of that expression.
What you need to do is test each of your text boxes individually, using if statements:
If Me.CustName = "" Then Me.CustName.BackColor = &H8080FF
'etc.
Related
This is part of my code:
For Each itm In IE.document.all
If itm = "[object HTMLInputElement]" Then
n = n + 1
var_dados = Empty
itm.Focus
itm.Click
Select Case n
Case 1
var_dados = Worksheets("Rosto").Range("d8")
Case 3
var_dados = Worksheets("Rosto").Range("d9")
'code here
itm.value = var_dados
And the data goes to the form, we see it there but when the form is submited and you go to see it, all fields are blank. What i´m doing rong?
I think i got a workaround
After this:
itm.value = var_dados
I used:
itm.Focus
itm.Click
Them used senkeys. The sendkeys needed to be some kind of text, case return or enter didn't worked.
And it worked
I am trying to write a conditional case statement that searches through a specific column, in excel, for a specific string and when it matches with the string that cell's background color is changed.
If the cell is empty or does not match the string then nothing should happen to the cell.
Right now I am trying to iterate through each cell in the column and check all possible string values to compare to but it does not seem to be working .
Here is my current code:
Sub interiorsStatus()
Dim sh As Worksheet
Dim rw As Range
Set sh = ActiveSheet
For Each rw In sh.Rows
Select Case sh.Cells(rw.Row, "E").Value
Case "DELIVERED"
result = Range(rw.Row).Interior.ColorIndex = 33
Case "READY TO ORDER"
result = Range(rw.Row).Interior.ColorIndex = 36
Case "ORDERED"
result = Range(rw.Row).Interior.ColorIndex = 39
Case "DELIVERED"
result = Range(rw.Row).Interior.ColorIndex = 43
Case "EXISTING"
result = Range(rw.Row).Interior.ColorIndex = 40
Case "ON HOLD"
result = Range(rw.Row).Interior.ColorIndex = 48
Case "GENERAL CONTRACTOR"
result = Range(rw.Row).Interior.ColorIndex = 2
Case "AV & BLINDS"
result = Range(rw.Row).Interior.ColorIndex = 15
Case "MILLWORK"
result = Range(rw.Row).Interior.ColorIndex = 22
Case Else
result = """"
End Select
Exit For
Next rw
End Sub
The line Exit For stops your iteration after the first time. I think, this is not, what you want. Or you have to write it inside the case-statement.
result = Range(rw.Row).Interior.ColorIndex = 40
That's an assignment. It's assigning to result, the value of the expression to the right of the assignment operator.
Range(rw.Row).Interior.ColorIndex = 40
When you have that on the right-hand side of an assignment operator, that's a Boolean expression, it evaluates to True or False. So result will be True when ColorIndex is 40, and False otherwise.
And then nothing gets done with the result.
If you intended to actually set the ColorIndex, remove the result = assignment to turn the Boolean expression into an assignment instruction that assigns ColorIndex.
And then there's the other problem: you explicitly exit the loop just before you finish the first iteration. Remove that Exit For if you want to actually loop.
I don't have a great deal of experience working with DataSets and haven't been able to find the best way of achieving what I want to achieve.
I basically create a DataSet using a SQL Query and then I am trying to find a Specific Value in the 'Field' column and then if there is a 'Y' in the 'Flag' (as apposed to a 'N') Column on the same Row then I want it to change a check box's state to Checked as well as updating a labels text.
What I have seems to work however if no data is returned I get the below error:
Object reference not set to an instance of an object
If I change the code slightly from .FirstOrDefault() to .First() I get this error:
Sequence contains no elements
The part of the code that appears to be causing the problem is listed below. If you need to know anything else I will add it in.
Dim sSQL As String
sSQL =
<SQL>
SELECT MAX(UpdateTime) AS UpdateTime FROM AdminCS_Data_Current
WHERE UpdateUser = |##UpdateUser|
</SQL>
sSQL = Replace(sSQL, "##UpdateUser", AdminCB.Text)
Me.LastUserUpdate.Text = "Last Action: " & Format(ReturnDatabaseValue(sSQL, "UpdateTime", "Data"), "dd/MM/yyyy HH:mm:ss")
Dim EmployeeDataset As New DataSet
Try
sSQL =
<SQL>
SELECT * FROM AdminCS_Data_Current
WHERE UpdateUser = |##UpdateUser| AND CONVERT(DATE, UpdateTime) = CAST(GETDATE() AS DATE)
ORDER BY UpdateTime ASC
</SQL>
sSQL = Replace(sSQL, "##UpdateUser", AdminCB.Text)
EmployeeDataset = ReturnDataSet(sSQL, "Data")
If EmployeeDataset IsNot Nothing Then
Dim eData = EmployeeDataset.Tables(0)
If (eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag")) IsNot Nothing Then
If eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag").ToString.Trim = "Y" Then
TShtY.CheckState = CheckState.Checked
TShtTime.Text = Format(eData.Select("Field = 'Timesheets Checked'").First()("UpdateTime"), "HH:mm:ss")
Else
TShtN.CheckState = CheckState.Checked
End If
End If
' The above two IF statements would be repeated several times on each change of "Field"
End If
It would appear that this code has introduced not just iunefficiency but also a bug:
If (eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag")) IsNot Nothing Then
If eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag").ToString.Trim = "Y" Then
TShtY.CheckState = CheckState.Checked
TShtTime.Text = Format(eData.Select("Field = 'Timesheets Checked'").First()("UpdateTime"), "HH:mm:ss")
Else
TShtN.CheckState = CheckState.Checked
End If
End If
It should have been written like this in the first place:
Dim row = eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()
If row IsNot Nothing Then
If row("Flag").ToString.Trim = "Y" Then
TShtY.CheckState = CheckState.Checked
TShtTime.Text = Format(row("UpdateTime"), "HH:mm:ss")
Else
TShtN.CheckState = CheckState.Checked
End If
End If
Easier to read, more efficient and avoids that nasty bug.
Also, I'd much rather see this:
Dim row = eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()
If row IsNot Nothing Then
If row("Flag").ToString.Trim = "Y" Then
TShtY.Checked = True
TShtTime.Text = CDate(row("UpdateTime").ToString("HH:mm:ss")
Else
TShtN.Checked = True
End If
End If
You should never use the CheckState of a Checkbox unless it's tri-state, which maybe yours are but I doubt it. As for Format, we're not in VB6 anymore Toto.
i'am using this formula for parameters (record selection) :
if {?customPort} <> "" then {ArrivingTrips.port} = {?customPort};
if {?airlin} <> "" then {ArrivingTrips.airlines} = {?airlin};
if {?hal} <> "" then {ArrivingTrips.hall}={?hal};
if (hasvalue({?tim})) then {ArrivingTrips.arrivalTime}={?tim};
{?d1} = {ArrivingTrips.arrivalDate};
the problem now, crystal report just looks at d1 and dosent care for another parameters even if the "if" expression is true so it shows unwanted data, why that ?
is it wrong syntax ?
Note : i send the parameters values by my vb.net code, not by Crystal parameters input panel.
Thanks.
local timevar t;
local stringvar air;
local stringvar port;
local stringvar halll;
if {?tim} <> "" then t:= ctime({?tim}) else t:= ctime({ArrivingTrips.arrivalTime});
if {?airlin} <> "" then air:= {?airlin} else air:= {ArrivingTrips.airlines};
if {?customport} <> "" then port:= {?customPort} else port:= {ArrivingTrips.port};
if {?hal} <> "" then halll:= {?hal} else halll:= {ArrivingTrips.hall};
{ArrivingTrips.airlines} = air and {?d1}= {ArrivingTrips.arrivalDate} and port={ArrivingTrips.port} and halll = {ArrivingTrips.hall} and (t)=ctime({ArrivingTrips.arrivalTime});
This is the solution, now , when "if condition" returns false result, report will care only for true conditions because the false one will will return the same value of a field so it's like no condition :)
Thanks for every one tried to help.
({?d1} = {ArrivingTrips.arrivalDate})
AND (If {?customPort} <> "" Then {ArrivingTrips.port} = {?customPort} Else TRUE)
AND (If {?airlin} <> "" Then {ArrivingTrips.airlines} = {?airlin} Else TRUE)
AND (If {?hal} <> "" Then {ArrivingTrips.hall}={?hal} Else TRUE)
AND (If (hasvalue({?tim})) Then {ArrivingTrips.arrivalTime}={?tim} Else TRUE)
You are writing your selection formula as single statements, so only the last statement in line gets evaluated. You should also have an "else" for each "if" condition, otherwise your statements will be prone to error and may show undesired data.
(if {?customPort} <> "" then {ArrivingTrips.port} = {?customPort} else {other.condition}) OR
(if {?airlin} <> "" then {ArrivingTrips.airlines} = {?airlin} else {other.condition}) OR
(if {?hal} <> "" then {ArrivingTrips.hall}={?hal} else {other.condition}) OR
(if (hasvalue({?tim})) then {ArrivingTrips.arrivalTime}={?tim} else {other.condition}) OR
({?d1} = {ArrivingTrips.arrivalDate})
This formula will evaluate if any of the conditions are met. If you need a combination of conditions met, then you will have to join them by using AND.
What is the best way to compare an Int entry from a table in a case statement?
Using SQL server 2008 R2, Visual Basic Express with LINQ to SQL.
The code I tried doesnt work:
Private Sub UpdateSetOpt()
Dim db = New ACEDataContext
Dim SRM = From q In db.Settings
Where q.SettingID = frmMain.CurrentSID
Select q.RollMethod
Select Case SRM
Case 1
rbStandard.Checked = True
Case 2
rbProfession.Checked = True
Case 3
rbSpecies.Checked = True
Case 4
rbRandom.Checked = True
Case Else
rbStandard.Checked = False
rbProfession.Checked = False
rbSpecies.Checked = False
rbRandom.Checked = False
End Select
End Sub
SRM isn’t an Integer since your From query returns a collection of items. To get just the first, use Single():
Dim SRM = (From q In db.Settings
Where q.SettingID = frmMain.CurrentSID
Select q.RollMethod).Single()
If the query actually returns more than a single value the above code will fail; you need to use First instead of Single then. Both will fail if no value is returned by the query. In that case, FirstOrDefault may be used instead (but probably isn’t appropriate in your situation).
Adding to that, your Select Case is a sign of code smell. You should rather create an array of all the check boxes and use the integer to map into it:
Dim checks As CheckBox() = New CheckBox() { _
rbStandard, rbProfession, rbSpecies, rbRandom }
' Unset all checkboxes:
For Each check In checks
check.Checked = False
End For
If SRM > 0 AndAlso SRM <= checks.Length Then
checks(SRM - 1).Checked = True
End If
SRM in your case is not an int but IEnumerabe<int>.
You want the first element:
SELECT CASE SRM.FirstOrDefault():