Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

vba - Textbox as Input in an SQL Query in Access

I am trying to create a database in Microsoft Access 2010 involving records of employees. One of my macros involves running the following SQL query:

SELECT
    *

FROM
    Employees

WHERE
    [First Name] = [Enter First Name]
    AND [Last Name] = [Enter Last Name];

Currently running the query results in 2 dialog windows popping up to enter the first and last name. This works as intended, but I want to bind the query to a button in my database next to some text boxes, like this, so that when I click on the blue button it runs the query with the text boxes as the first and last name inputs. I'd like to not get too deep into VBA if I can help it, as I haven't had any experience coding in it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No code needed - as @Nathan_Sav said a - a quick search will find what you're after (I find "reference main form from subform" gives the best page at the top - http://access.mvps.org/access/forms/frm0031.htm )

SELECT *
FROM Employees
WHERE [First Name]=Forms![Form1]![txtFirstName] AND 
      [Last Name]=Forms![Form1]![txtLastName]

txtFirstName & txtLastName are the names I gave to the text controls on Form1.

Edit: Your macro button can then just open the query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...