VBA isnumber istext function problem - function

im trying to check if a number is a text or number in a banking system. If the value is incorrect it should erase and send an error message, but my code isnt working at all... can someone help me? here is the code:
Private Sub TextBox1_Change()
name = TextBox1
If Application.IsText(name) = True Then
IsText = True
Else
MsgBox "Insert only words"
Selection.name.Delete
End If
End Sub

I think you should be using name = TextBox1.Text instead. It's probably giving you an error on that line.
Also, you may want to wait until the user is finished typing the response, I believe the changed event will run each time a character is pressed. The AfterUpdate will run after you tab or click away from the textbox.
And really, instead of deleting the response (which I don't think will work), you should simply set the textbox blank back to an empty string.
Private Sub TextBox1_AfterUpdate()
If Not MyIsANumber(TextBox1.Text) Then
MsgBox ("It's Text")
Else
TextBox1.Text = ""
End If
End Sub
Function MyIsANumber(str As String) As Boolean
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
MyIsANumber = True
Exit Function
End If
Next
MyIsANumber = False
End Function
I'm sure the code could be structured better, but there's something along the lines of what you need. In this case, it just wipes the textbox if there is a number entered. Obviously, things will need to be the opposite for your textbox that you want to contain a number.

Have you considered the vartype() function? https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function

Related

How to ignore MS Access error 2113 totally?

I have a textBox formatted as "Short Date". When I put invalid data in field, for example random "dfsdf", and try to change focus, form throws validation Error 2113.
My goal is to give an opportunity to user to close form by click on "Cancel" button without any problem, because no matter what he entered in Date textbox while form is canceled.
I can handle this error and disable message with Form_Error event, but focus stays set to date textBox anyway, when i try to click Cancel button, and button is never clicked.
I use a setup, probably similar to yours:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const InvalidDate As Long = 2113
Select Case DataErr
Case InvalidDate
MsgBox "Invalid date!"
Response = acDataErrContinue
' Cancel invalid entry.
Me.Undo
' DoCmd.Close
Case Else
Response = acDataErrDisplay
End Select
End Sub
Problem is, that this will fire before your button gets focus. This means, that the form doesn't even know what you clicked, only that it was somewhere outside the date textbox.
Thus, the DoCmd.Close would close the form for whatever was clicked.
You may turn it into an advantage, that the user is able to watch the removal of the faulty input ...
You should use the after update event to check if the user entered a valid date,
if not then clear or undo. Without knowing the controls of your form i can't tell if you allow users to change table values or if you are using transactions.
pseudo code:
Private Sub Text1_AfterUpdate()
If Check_If_Date Then
Execute_Code
Else
End If
End Sub
Private Function Check_If_Date() as Boolean
If IsDate(Text1) then
Check_If_Date = True
Else
Check_If_Date = False
Select Case msgbox("Not a valid date", vbOKCancel)
case vbOK
reset_value
case vbCancel
Close_Form
case else
End Select
End If
End Sub
Private Sub Reset_Value()
'need to clear value or undo
End Sub
Public Sub Execute_Code()
'Code to save values or allow Update to complete
End Sub
Private Sub Close_Form()
'Code For Closing Form and logging errors
End Sub
I split up repeatable actions for reuse in case the same thing can be repeated in another text box for example a second date or to get the same behavior for the other controls.

Suppress standard error message with predefined choice

I have built a form in MS Access that creates datasets for a database.
I have then implemented some code to check if all required fields have been filled before permitting the form to be closed and the dataset to be saved to the database.
All is working well, but I can't seem to suppress the standard error message created by Access telling me that I cannot save the dataset at the moment (if a required field is not filled and Cancel is set to true) and aksing me if I wanted to exit the form without saving.
The structure of my code is as follows:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If requiredFieldsNotFilled Then
Cancel = True
Else
Cancel = False
End If
End Sub
When Cancel is set to true (i.e. the required fields haven't all been filled) a standard error message is thrown by Access (Error Code 2169) which I am trying to suppress as such:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
End Sub
However, the generated error message provides a yes/no choice. 'Yes' being close the dataset without saving, 'No' being not close the dataset.
If I use the error handler as described above the default choice is taken which in this case is 'Yes' and the dataset is closed without being saved!
This is not what I want! I want 'No' to be chosen so that the user stays on the form and can correct his inputs... Any ideas on how to do that?
Any help is much appreciated! Thanks in advance!
I don't think such functionality is available, but you can manually stop the next close action by using the Form_Unload event:
Dim DontClose As Boolean
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then
Response = acDataErrContinue
DontClose = True
Else
Response = acDataErrDisplay
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cancel = DontClose
DontClose = False
End Sub

Alerting a user that two fields are the same

Is there any way I can let a user know that the field in the current record is identical to the field in another record? Because 99% of the time they will be different, but 1% of the time there will have to be two records with the same field, so I want a way to alert the user and just make sure they are aware of that in case they did not mean to do it.
Write a Function and call it as needed. In your case in the AfterUpdate of the control as #Andre mentioned above.
Public Function IsDuplicate(ByVal Value As String) As Boolean
IsDuplicate = (DCount("*", "TableName", "FieldName='" & Value & "'") > 1)
End Function
You can call it like this:
Private Sub Text0_AfterUpdate()
If IsDuplicate(Me.Text0.Value) Then
MsgBox "Value exists..."
End If
End Sub

I need to trigger Application.WorksheetFunction.VLookup from textbox updated externally

I have a form that gets updated from a web page scrapping.
One of the entries is missing and so I use a value from a textbox to do a vlookup to get the missing info and update the empty textbox.
None of the event methods fire automatically!
Private Sub envelope_afterupdate()
On Error Resume Next
MsgBox ("test")
name_env.text = Application.WorksheetFunction.VLookup(envelope.text, Worksheets("DATA_name").Range("a1:b334"), 2, False)
If Err.Number <> 0 Then
' MsgBox "currName not found" ''optional, no need to do anything
End If
End Sub
Any suggestions?
I simply put the vlookup in my data extraction code!
Form1.name_env = Application.WorksheetFunction.VLookup(form1.envelope, Worksheets("DATA_name").Range("a1:b334"), 2, False)
Pete

Debug for an if statement in Access 2013

I have a piece of code to make sure the user enters a "scrap amount" if they have entered a "scrap code". When I apply and use the code for Scrap1, everything works great. When I try to apply that same code with the next set of variable names, I get an error that says my code "has an ambiguous name detected. I have checked my variable names and the code until my eyes bled. Everything looks fine to me. Does anyone see an error that I have missed?
'check to see that there is a scrap amount if a code has been entered #1.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ScrapCodes1.Value Then
If Me.ScrapAmount1 = 0 Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
End If
End If
End Sub
'check to see that there is a scrap amount if a code has been entered #2.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ScrapCodes2.Value Then
If Me.ScrapAmount2 = 0 Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
End If
End If
End Sub
Yes, assemble in one sub:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' check to see that there is a scrap amount if a code has been entered #1.
If Me.ScrapCodes1.Value Then
If Me.ScrapAmount1 = 0 Then
Cancel = True
End If
End If
' check to see that there is a scrap amount if a code has been entered #2.
If Me.ScrapCodes2.Value Then
If Me.ScrapAmount2 = 0 Then
Cancel = True
End If
End If
If Cancel = True Then
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
end If
End Sub