Run query if value changes.

Reeds

New Member
Joined
Mar 10, 2003
Messages
7
I have a form with two textboxes.
Also I have a button on this form.
If I push the button: value textbox1 = value textbox2

What I want is that if the value of textbox1 changes a macro will run and a query will add both values to a table.

This should work with the "On Change" event of textbox1.

If I change the value of textbox2 and push the button, the value of textbox1 changes, but the macro (query) won't run. Access does't recognize the change, probably because the value isn't entered through the keyboard.

How can I solve this?

(In Excel I have the same problem with linked cells)
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You might want to put your code that updates your table into its own sub procedure and then have the OnChange event call it up when the value changes and have the command button call it up when you click it.

I know this by passes the OnChange event, but I have had similar issues when wanting to run the same code by either an event or by a user's action. Hope this helps.
 
Upvote 0
Hi DRB,

You could compare the value of textbox1 before it gets textbox2's value, to the value after it gets it.

Something like:
Code:
cmdbutton1_click()

dim firstval as variant

firstval = textbox1
textbox1 = textbox2
if textbox1 <> firstval then call macro1

end sub

HTH,
 
Upvote 0
Yes that works great.

I noticed that one thing has to be added to the OnChange event, so that when someone does change the value in the textbox1 it grabs the changed value and not the old value. The Refresh command has to be added before calling up the sub.

Private Sub Textbox1_Change()
Me.Refresh
Call MyQuery
End Sub

Sub MyQuery()
MsgBox Me.Textbox1 & " " & Me.Textbox2
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,517
Messages
6,160,266
Members
451,635
Latest member
nithchun

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top