• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

Populate form based on a text box

Hi Experts-

I have a form with in access which is pulling data from Ms Sql thru ODBC connection (a View). What i want is to have a text box as a search field where the user types in a number or part of a number and it will only display the relevant results in the form. I have done this before with a combo box but not with text box. What i have is right now when i open the form it pops up  a dialog saying enter parameter value (which is my text box used for searching) then i put in some number and it displays the info on the form. What i want to do is to be able to go directly to the form where i have the text box. i put in the number there and gives me the results and then i should be able to look up for another number. On my form record source i have the following query

SELECT * FROM myview WHERE Num=txtSearch.value or right(Num,5)=txtSearch.value; and the other text boxes where i see the results they all have their control source set to the respective fields(columns) of the view.  Any kind of help would be appreciated. Thanks
3 Solutions
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Just reset the RecordSource of your form when you need to search ... for example, if you have a Search button, and want to refer to a textbox:

Sub cmdSearch_Click()
  Me.Recordsource = "SELECT * FROM myview WHERE Num=" & txtSearch & " OR right(Num,5)=" & txtSearch.value
End Sub

This assumes Num is a numeric field; if not, then you'd need to surround it with single quotes.
Delete the Form Source from the Form Source box
In the properties for your Textbox create an After  Update Event for your textbox

Set the  After Update Event to

Me.Form.RecordSource = SELECT * FROM myview WHERE Num=txtSearch.value or right(Num,5)=txtSearch.value

If your form now provides the parameters pop-up box, all you need do is place the textbox on the form, and replace the parameters portion of the query behind the form with the textbox reference:

Select * from myView where Num = [EnterNum];


Select * from myView where Num = Forms!myFormName!txtSearch

Now when you open the form the textbox is empty so the form should be clear.  Now when you place a value in the textbox you want the form to re-query so add this code to the AfterUpdate event of the textbox:

Private Sub txtSearch_AfterUpdate()
End Sub
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

zafridiAuthor Commented:
none of the above works for me...i guess u guys didn't quite get what i was looking for...simply put if i want to display values in a form(in text boxes) based on another text box that exists on the same form..how is it get done.. or i could have a subform that displays the results based on a textbox (exists on another form)..i think it should be pretty simple..i've done this before from a combo box but couldnt get it done from a text box..im not sure why. i hope i explained my question properly..thanks
Do you now have a form, based on a parameter query which asks for a value for Num?  That's certainly what your question reads like.   On that same form you create a textbox named txtSearch, correct?.  You modified the query, and inserted the code? What happened when you ran my answer?
zafridiAuthor Commented:
This is what i have in the form record source now

SELECT * FROM dbo_EquipmentforAccessVw WHERE EquipSerialNum=Forms!frmSearchOptions!txtSearchVin;

and the text box after update event  Me.Requery

when i run it..the form opens but nothing shows...its just blank...not even the labels..no text nothing...thankss
I see the problem.  An Access ODBC connection to a SQL table is processed in the SQL server.  It knows nothing about the local Access Form.  Somehow you have to get the value from the local form textbox control into something understood by the SQL parser - @varvalue ??
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now