Get text value from form in Access using VBA module

Currently, I want to get a value from a form in order to set it in an SQL query that I am making in a module under Access using VBA. I tried to use

value = Forms![NameOfForm]![NameOfTextbox] sqlquery = "...." & value & "....." 

This is a mistake (2450) saying that it cannot find the specified form. How can I get the value of this text box so that I can use it in my module?

thanks

+8
vba ms-access forms
source share
1 answer

Modify the VBA code to make sure the form is open.

 DoCmd.OpenForm "NameOfForm" 

This should prevent error # 2450.

Subsequently, you do not need to save the [NameOfTextbox] value in a variable, and then use this variable to create your SQL statement. You can use its value directly.

 sqlquery = "SELECT * FROM YourTable " & _ "WHERE some_field = '" & Forms![NameOfForm]![NameOfTextbox] & "';" 

Or insert a link to the text field (instead of the value of the text field) in the request.

 sqlquery = "SELECT * FROM YourTable " & _ "WHERE some_field = Forms![NameOfForm]![NameOfTextbox];" 

I assumed that some_field is a text data type field, so I enclosed the value of the text field with single quotes in the first query example. Note that the second example does not need quotation marks, because it refers to a text field by name, not its value.

However, if you continue your original approach (keeping the value of the text field for the variable), do not call the variable "value", because the value can be confused with the property of many objects.

+6
source share

All Articles