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

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

Related

Clear entry from Access Form UI but not DB, after saving record

I will enter roll, grade and data entered by and click Save Record to save record in DB. I want the Clear button to only clear the entry in UI of Form and not in DB, so I can enter a new record instead of closing the form and opening it again to make new entry.
The UndoRecord macro works on the Form UI, only if the record hasn't been saved in DB. Also I dont want to delete the record, I just want to clear the entry from the Form UI after saving it.
Here's a basic recipe for a bound form:
Set the form's DataEntry property to true (for instance in the form's Property Sheet).
Change the form's RecordSource query to contain an always-false statement in the WHERE clause, for example something like
SELECT Data.* FROM Data WHERE (True = False);
In the [Save Record] button click event handler, include code similar to the following:
Private Sub SaveRecord_Click()
On Error Resume Next
Me.Dirty = False 'Attempt to save the record
If Err.Number = 0 Then
Me.Requery 'Force the form to reload the query and reset the data entry form
Else
MsgBox Err.Description, vbCritical, "Error"
End If
End Sub
Note: The false criteria and setting DataEntry = True are actually redundant, since one or the other should be sufficient to keeping existing records from being shown in the form. However, certain key combinations could allow navigation of the form whilst showing recently added records. Having both of the settings ensures that only new records can be added without showing any saved records.
If you only want to clear the form with the clear button, leaving newly-added records available to review, then instead setup the following code:
Private Sub SaveRecord_Click()
On Error Resume Next
Me.Dirty = False 'Attempt to save the record
If Err.Number = 0 Then
'Only enter a new record, leaving other newly-added record accessible
DoCmd.GoToRecord , , acNewRec
Else
MsgBox Err.Description, vbCritical, "Error"
End If
End Sub
Private Sub ClearForm_Click()
On Error Resume Next
Me.Dirty = False 'Attempt to save the record
If Err.Number = 0 Then
Me.Requery
Else
MsgBox Err.Description, vbCritical, "Error"
End If
End Sub

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

Message box that confirms if a user wants to leave a form

I had a piece of VBA running, and for some reason it has suddenly stopped working.
On my form unload event I have the code:
if isnull(me.field) then
ans=MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
If ans=vbNo then
Cancel=True
end if
end if
This worked for a couple months, when the user exited the warning message would appear, and if they select no the form would not exit. Now when I click no I get an error:
Run time Error 3270. Property not Found
I changed the code to:
if isnull(me.field) then
ans=MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
If ans=vbNo then
docmd.cancelevent
end if
end if
Now I get error:
Runtime Error '2001' You Canceled The Previous Operation
Which is what I want.
How do I get a messagebox to confirm that a user wants to exit a form?
Edit: I realize that the exit warning works when I exit the form by pressing x in the upper right, but when I exit using a button with the docmd.close I get the errors. Any way around that?
Simply use the button click event.
Where CommandButton14 is the name of your exit button.
EDIT for users comment.
Have your exit button call the UserForm_QueryClose event.
Private Sub CommandButton14_Click()
'UserForm_QueryClose 0, 0
Unload Me
End Sub
Ask your question in that event. If they say yes end the app or unload the form. If they say no, cancel.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim Response As Integer
' Displays a message box with the yes and no options.
Response = MsgBox("Warning you have not entered all the data. Do you want to exit the form?", vbYesNo)
' If statement to check if the yes button was selected.
If Response = vbYes Then
'Cancel = False
End
Else
Cancel = True
End If
End Sub
You can take everything out of your unload event.
I feel like I am missing something because the path you chose seems a little over complicated.
Firstly, with my database I always make sure to set all of my forms Close Button property to "No" this way you always have control of when the user closes the form.
So from that point you just need this code attached to your close button:
Private Sub btnClose_Click()
Dim blnClose As Boolean
Dim strResponse As String
'Default to true so it always closes unless one or more future checks fail
blnClose = True
If IsNull(Me.Field) Then
strResponse = MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
'User wants to cancel close toggle Boolean to false
If strResponse = vbNo Then
blnClose = False
End If
End If
'If nothing has toggled to false then close the form
If blnClose = True Then
DoCmd.Close , ""
End If
End Sub

Dirty event not firing on Access 2007 form

I have a bound pop up form with a Clear data button. It runs the Undo command.
The Form_Current event sets this button's enable property to False.
The Form_Dirty event sets this button's enable property to True.
The button's property is always set to False even after I enter data into the form. I think this is because my Form_Load event is populating two fields. One is passed from the main form as an OpenArgs, the other is the unique ID which is calculated based on the OpenArgs value.
Any suggestions as to how to get the Dirty event to activate under these circumstances? If not, then an alternative approach perhaps?
Thanks in advance.
Code below:
Private Sub cmdUndoChanges_Click()
CustID_temp = Me!CustID
FacNo_temp = Me!Unique_ID
DoCmd.RunCommand acCmdUndo
Me!CustID = CustID_temp
Me!Unique_ID = FacNo_temp
End Sub
Private Sub Form_Current()
Me!cmdUndoChanges.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me!cmdUndoChanges.Enabled = True
End Sub
Private Sub Form_Load()
Dim test As Integer
Me!CustID = OpenArgs
test = DCount("Unique_ID", "tbl_Table2", "CustID = '" & Me!CustID & "'")
If IsNull(Me!UNIQUE_No) Then
test = test + 1
Me!Unique_ID = CustID & "-" & test
MsgBox "No Previous Data"
Else
' these will be turned back on when the user selects the
' modify data button or add new data button.
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acOptionGroup, acCheckBox
ctl.Locked = True
ctl.BackColor = 15066597
Box40.BackColor = 15066597
End Select
Next ctl
End If
MsgBox Me!Unique_ID
End Sub
Have you tried Form_BeforeInsert() instead of Form_Dirty()?
I found another case where the Form_Dirty event never fires, the hard way. I thought I'd add it here since this is the top search result for the "not firing" question.
If your Form_Load function does anything to change the value of an existing record, the form will be set to dirty immediately, apparently before the logic that fires the event begins to run. Because of that there will never be an event if other data on the form is changed.
We had a "smart" Save button that was disabled until Form_Dirty fired. After I added a fixup to Form_Load for old records with a junk field, editing those records no longer ever enabled the Save button.

VBA isnumber istext function problem

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