Taking user back to specific textbox - vba

I have a little routine that checks for the time input... ie if a user enters "8", it will change this to 08:00 etc etc... works great.
Now I thought I'll be smart and make sure that the user enters a max of 4 number and if not, a msgbox pops up. so far easy enough but how on earth do I take him back to the textbox so he can correct his entry ? I tried .Setfocus but nothing happens ?? any ideas ?
Private Sub ZB_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(ZB) = 2 Then
ZB = ZB & ":00"
ElseIf Len(ZB) = 1 Then
ZB = "0" & ZB & ":00"
ElseIf Len(ZB) = 4 Then
ZB = Left(ZB, 2) & ":" & Right(ZB, 2)
ElseIf Len(ZB) = 3 Then
ZB = Left(ZB, 1) & ":" & Right(ZB, 2)
ElseIf Len(ZB) > 4 Then
MsgBox "What you trying to say ???"
ZB.SetFocus
Else
End If
End Sub

Santosh's suggestion is great for limiting the characters allowed in the TextBox. That alone may resolve this problem. But here is an answer for others who may not be able to use a character limit as readily as you can in this example.
To return focus to the Textbox ZB, do this:
Cancel = True
ZB.Value = vbNullString
ZB.SetFocus
The Cancel = True is the part you're missing. This essentially cancels out the Exit event, then I force the TextBox value to be blank, and then .SetFocus.

Related

Check for duplicates in a text form field

I am a VBA noob and I'm trying to compare the numbers entered in a text form field in ms word, then if a duplicate is found I display a msg box.
The problem I'm having is... I have to compare 33 fields and look for a duplicate of anywhere from 1 to 33. I'm receiving a message that the procedure is too large. I'm sure there must be an easier way to do this. It's a protected document which I unprotect once the user hits the command button. I've even tried breaking it up and assigning macros to some of the fields.
Here is a sample of what I have. There are 33 bookmarks and I'm comparing each field.
If (ActiveDocument.FormFields("s1").Result = "1" And _
ActiveDocument.FormFields("s2").Result = "1") Then
MsgBox ("Your preferences cannot be duplicated.")
Validate = True
If True Then Exit Sub
Else
Validate = False
End If
Perhaps:
Sub CompareFormfields()
Dim i As Long, j As Long
With ActiveDocument
For i = 1 To .FormFields.Count - 1
For j = i + 1 To .FormFields.Count
If (.FormFields(i).Type = wdFieldFormTextInput) And (.FormFields(i).Type = wdFieldFormTextInput) Then
If .FormFields(i).Result = .FormFields(j).Result Then
MsgBox "The data in FormField " & i & " is duplicated in FormField " & j
End If
End If
Next
Next
End With
MsgBox "Done checking"
End Sub

My VBA for loop in MS word 2016 is not working

I having trouble with the following code in MS word VBA
For i = 6 To ActiveDocument.Tables(2).Rows.Count
z = Len(ActiveDocument.Tables(2).Cell(i, 2).Range.Text) - 2
x = Len(ActiveDocument.Tables(2).Cell(i, 3).Range.Text) - 2
b = ActiveDocument.Tables(2).Cell(i, 5).Range.ContentControls.Item(1).ShowingPlaceholderText
If (z = 0 And x = 0) Then
If b = True Then
MsgBox "Please do error 1!"
If vbOK Then
Exit Sub
End If
Else
MsgBox "Please do error 2!"
If vbOK Then
Exit Sub
End If
End If
Else
If b = True Then
MsgBox "Please do error 3!"
If vbOK Then
Exit Sub
End If
Else
Confirm = MsgBox("Are you sure to submit?", vbYesNo, "Confirmation")
If Confirm = vbNo Then
Exit Sub
End If
End If
End If
Next i
The for loop won't go into the second line to check if z or x is having value or not
I doubt moving Next i would have solved anything. This code is riddled with badness.
My impression is that your code is intended to check three columns in a table (from rows 6 downwards) - this appears to be a consistency check.
Naming. z, x and b are not very descriptive. Using names like lengthCol2, lengthCol3 and hasPlaceHolderText will help you follow your logic more closely.
Use Option Explicit. Always.
You use a standard MsgBox call, which by default only has a single button ("OK"). The MsgBox is a blocking code element, so the macro will not progress until the user has clicked "OK".
vbOK is an enumerated value (value = 1). So If vbOK then always comes out true. Always. You appear to be seeking some sort of user input, but you are not clear on what that input is.
Address these simple steps gives us:
For i = 6 To ActiveDocument.Tables(2).Rows.Count
lengthCol2 = Len(ActiveDocument.Tables(2).Cell(i, 2).Range.Text) - 2
lengthCol3 = Len(ActiveDocument.Tables(2).Cell(i, 3).Range.Text) - 2
hasPlaceHolderText = ActiveDocument.Tables(2).Cell(i, 5).Range.ContentControls.Item(1).ShowingPlaceholderText
If (lengthCol2 = 0 And lengthCol3 = 0) Then
If hasPlaceHolderText = True Then
MsgBox "Please do error 1!"
Exit Sub
Else
MsgBox "Please do error 2!"
Exit Sub
End If
Else
If hasPlaceHolderText = True Then
MsgBox "Please do error 3!"
Exit Sub
Else
Confirm = MsgBox("Are you sure to submit?", vbYesNo, "Confirmation")
If Confirm = vbNo Then
Exit Sub
End If
End If
End If
Next i
Your logic is negative-biased - that is, intending to find reasons not to do something than to do something. Positive-biased logic is usually easier to understand and maintain - the coder's intent is clearer.
Rewording the logic gives us:
For i = 6 To ActiveDocument.Tables(2).Rows.Count
lengthCol2 = Len(ActiveDocument.Tables(2).Cell(i, 2).Range.Text) - 2
lengthCol3 = Len(ActiveDocument.Tables(2).Cell(i, 3).Range.Text) - 2
hasPlaceHolderText = ActiveDocument.Tables(2).Cell(i, 5).Range.ContentControls.Item(1).ShowingPlaceholderText
If (lengthCol2 > 0 OR lengthCol3 > 0) AND hasPlaceHolderText Then
Confirm = MsgBox("Are you sure to submit?", vbYesNo, "Confirmation")
If Confirm = vbYes Then
'Do submission code here - or call the submission procedure
End If ' Just do nothing if they say "No" - this is what your current code does.
Else
' The next line could be used instead of the nested IF-the-else statements following.
'MsgBox " Table contents are not valid, please ensure columns 2,3 and 5 are completed"
If hasPlaceHolderText then
If (lengthCol2 = 0 And lengthCol3 = 0) Then
MsgBox "Please do error 1!"
Else
MsgBox "Please do error 2!"
EndIF
Else
MsgBox "Please do error 3!"
End If
End If
Next i
Note that in your logic, either Column 2 or Column 3 can be empty and (as long as placeholder text not being shown) you document is ready for submission. Perhaps you meant AND instead of OR (i.e. all columns should be filled).
There is still one problem. Your loop. As currently written, you loop over the logic, thus you ask the user to either check errors or submit the document x number of times based on error checking in each row. But, just moving the Next i does not solve the problem because the only results that are retained are those in the last row. In other words, all the previous rows could be bad/invalid, but you would still be able to submit.
We can fix this last bit by creating cumulative logic. In other words, we track the errors in a short loop, then go into the main logic. This seems to be a little more complex but it really is relative straight forwards. But, we do need more Booleans to make it work.
Dim rowsOK as Boolean
'explicit initialisation - I am working on a positive bias here.
rowsOK = True
For i = 6 To ActiveDocument.Tables(2).Rows.Count
Dim lengthCol2OK as Boolean ' Use these just to make the logic clearer and the code cleaner
Dim lengthCol3OK as Boolean
Dim hasPlaceHolderTextOK as Boolean
lengthCol2OK = Len(ActiveDocument.Tables(2).Cell(i, 2).Range.Text) > 2
lengthCol3OK = Len(ActiveDocument.Tables(2).Cell(i, 3).Range.Text) > 2
hasPlaceHolderTextOK = ActiveDocument.Tables(2).Cell(i, 5).Range.ContentControls.Item(1).ShowingPlaceholderText
rowsOK = rowsOK And ((lengthCol2OK Or lengthCol3OK) And hasPlaceHolderTextOK) ' Note: Using "Or" here as per original code logic
' Extra logic could go here to message the user if any of the above are false.
Next i
If rowsOK Then
Confirm = MsgBox("Are you sure to submit?", vbYesNo, "Confirmation")
If Confirm = vbYes Then
'Do submission code here - or call the submission procedure
End If ' Just do nothing if they say "No" - this is what your current code does.
Else
MsgBox " Table contents are not valid, please ensure columns 2,3 and 5 are completed"
End If
However, this logic works on all the rows, so identifying individual row errors in not possible in the main loop. You could work extra logic in the For-Next loop to message the user for errors.
Now the code is maintainable, and more likely does what you want.
Key points:
Use Option Explicit. This prevents typos and ensures that you are using variables in they way you intend.
Use meaningful variable names. Makes it easier to follow what you want to do.
Don't confuse enumerated values with returns from functions. Don't confuse constants with variables.
Take some time to review your logic chains to ensure they do what you want to do, rather than not do what you don't want to do. The latter has greater chance of missing a non-valid path.

Access VBA ListBox ItemData Variant is always 1

I have a form with multiple list boxes and all work fine save 1! This is the only list box whose source is number data-type. I know this shouldn't matter, but what I'm seeing is that for this list box only the variant returned is always 1, and I cannot understand why the others (data-type text) work properly and this one doesn't. All of my Google searches and MSN searches and here on StackOverflow have not helped my specific issue though there's a LOT out there about ListBoxes. Please help!
Edit: Sorry #Mat's Mug...I was hoping that wouldn't be necessary as it's lengthy with all the checking going on, but here's the gist.
For Each ctl In Form.Controls
With ctl
Select Case .ControlType
Case acListBox
If .Visible = True Then
.SetFocus
ItemCount = .ItemsSelected.Count
If .ItemsSelected.Count > 0 Then
For Each varItm In .ItemsSelected
If .Name = "lstRating" Then
sWhereClause = sWhereClause & "ThisRating=" & .ItemData(varItem) & " Or ThatRating = " & .ItemData(varItem)
Else
sWhereClause = sWhereClause & Mid(.Name, 4, Len(.Name)) & " = """ & .ItemData(varItm) & """"
End If
Next varItm
End If
End If
End Select
End With
Next ctl
Note: When .Name = "lstRating" is True is the line where varItem returned is 1 regardless of what is selected. The list box is populated with values from 1 to 5 in 0.5 increments.
Well, I can't believe I was overlooking it for hours...I was using varItem in the offending line when it's defined at varItm, no "e"! TOTALLY an oversight on my part. Thanks all for looking into this!

More efficient error catching?

So I am at an internship for school that is having me code a program using VB.NET. One of the forms has three combo boxes that must have a choice selected in each before moving on. If the user missed one of the comboboxes I am trying to bring it to their attention to go back and make a selection before continuing. I have an if statement, which works exactly how I want it to:
If cboYear.SelectedIndex = -1 Then
warningString = warningString + "Year" & vbNewLine & "Vendor" & vbNewLine & "Report"
txtYear.ForeColor = Color.Red
TextBox7.ForeColor = Color.Red
txtReport.ForeColor = Color.Red
MessageBox.Show(warningString)
ElseIf cboVendorName.SelectedIndex = -1 Then
warningString = warningString + "Vendor" + vbNewLine & "Report"
txtYear.ForeColor = Color.Black
TextBox7.ForeColor = Color.Red
txtReport.ForeColor = Color.Red
MessageBox.Show(warningString)
ElseIf cboReport.SelectedIndex = -1 Then
warningString = warningString + "Report" & vbNewLine
txtYear.ForeColor = Color.Black
TextBox7.ForeColor = Color.Black
txtReport.ForeColor = Color.Red
MessageBox.Show(warningString)
Else
Main.Show()
Me.Hide()
End If
(warningString is a generic string that says something like "Please fill in the following:")
So like I said this works exactly how I want it to, and its pretty easy to follow my logic here. My question is how can I make this code more efficient? I have tried arrays to hold combobox selections, and I have tried a for loop and a select case to try and streamline the code to no avail. The array always breaks out and returns a NullReferenceException before it gets to my Select Case or For loop to see if there is a value in the combobox or not.
If there is someone out there who can help walk me through the logic of getting some kind of loop to work for the above, that would be awesome. I'm not asking for working code or anything like that, however if there is a solution to be found and I get there myself I will post it here for future reference for people. Who knows, this might even be the most efficient way already?
Edit: Plutonix has been gracious enough to have offered some comments on my question, and he brought up a point about the if statements not being mutually exclusive. That is because the comboboxes are being populated by datasets, and each CBO in order determines what information from the datasets will populate the next one. So, depending on what year is selected in the 'Year' combobox, determines which vendors populate the 'Vendor' combobox, and that determines which reports are populated in the 'Report' combobox.
Since each test is doing something different a loop would be clumsy. Even if you only enable B once A is validated, you might need to allow for them to UnDo a previous selection. The code can be condensed though:
Dim IsValid As Boolean = True
txtYear.ForeColor = Color.Black
TextBox7.ForeColor = Color.Black
txtReport.ForeColor = Color.Black
If cboYear.SelectedIndex = -1 Then
warningString = warningString & "Year" & vbNewLine & "Vendor" & vbNewLine & "Report"
txtYear.ForeColor = Color.Red
TextBox7.ForeColor = Color.Red
txtReport.ForeColor = Color.Red
IsValid = False
End If
If cboVendorName.Enabled AndAlso cboVendorName.SelectedIndex = -1 Then
warningString = warningString & "Vendor" & vbNewLine & "Report"
TextBox7.ForeColor = Color.Red
txtReport.ForeColor = Color.Red
IsValid = False
End If
If cboReport.Enabled AndAlso cboReport.SelectedIndex = -1 Then
warningString = warningString & "Report" & vbNewLine
txtReport.ForeColor = Color.Red
IsValid = False
End If
' See note
If IsValid = False Then
MessageBox.Show(warningString)
Else
' DONT use default form instances!
Main.Show()
Me.Hide()
End If
The IF changed a bit: I dont know how you tell which ones are active, the code above is using .Enabled to see if each has been set up for use. That way you wont report errors on CBO 2 and 3 when validating #1.
I would make it a function which Returns IsValid and let the calling code manage the forms to show...and dont use default form instances.

Excel VBA make a script asynchronous

I have a script that can ping a list of computers and change their background color depending after the result it gets.
My problem is, that it blocks the entire excel file while it runs.
So my question is, how can I make it to run async?
Here is the code:
'ping
Function sPing(sHost) As String
Dim oPing As Object, oRetStatus As Object
Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & sHost & "'")
For Each oRetStatus In oPing
If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
sPing = "timeout" 'oRetStatus.StatusCode <- error code
Else
sPing = sPing & vbTab & oRetStatus.ResponseTime & Chr(10)
End If
Next
End Function
Sub pingall_Click()
Dim c As Range
Dim p As String
Application.ScreenUpdating = True
For Each c In ActiveSheet.Range("A1:N50")
If Left(c, 7) = "172.21." Then
p = sPing(c)
If p = "timeout" Then
c.Interior.ColorIndex = "3"
ElseIf p < 16 And p > -1 Then
c.Interior.ColorIndex = "4"
ElseIf p > 15 And p < 51 Then
c.Interior.ColorIndex = "6"
ElseIf p > 50 And p < 4000 Then
c.Interior.ColorIndex = "45"
Else
c.Interior.ColorIndex = "15"
End If
End If
Next c
Application.ScreenUpdating = False
You can't do too much about this unfortunately since VBA runs in a single thread.
You can however introduce a degree of responsiveness by putting
VBA.DoEvents()
in various places in your code, ideally in the tight loops. In your case, put them just after the lines containing For. This pauses the VBA and flushes the event queue which will have the effect of making Excel responsive.
(Toggling the screen updating is a bad idea since you might leave things in a bad state if the function terminates unexpectedly. I'd remove the lines that do that if I were you.)
Excel can calculate "asynchronously". Call sPing as a function.
I'm not sure why your range is A1:N50. I assume one of the columns is the IP address, which I will assume as A. So your formula in column M will look like =sPing(A1).
As for the color coding, you can use conditional formatting.
While strictly speaking you cannot make Excel to behave as you need, there is a trick to work around it. The solution is to create another Excel instance. Then you can run the macro in one of the instances and work independently in the other one.
You can open another Excel instance from the Run prompt (press Windows + R) and then type Excel /x, then Enter