0

Fairly new to VBA. I have a list box on a form within Access which is populated with data from a table. Selecting a value from the list box gives and ID which is then used to perform a query. I need this ID to be available for use in another form to perform a query based on the Value. What is the best way of achieving this?

`Dim IDValue As String
IDValue = Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT * FROM Companies WHERE Companies.CompanyID = " & IDValue`
2
  • Dim IDValue As String Change it to Public IDValue As String and place it in a module. Commented Sep 27, 2013 at 16:11
  • 1
    You could simply add the ID to the query output and then continue to reference the query results. Alternately (and I wouldn't really recommend this next way), you can create a public (global) variable by declaring it at the top of a standard module and not within any subroutines: Public IDValue As String. Then you would assign a value to the public variable like you would normally, but it will be available for all forms to use. Commented Sep 27, 2013 at 16:11

3 Answers 3

2

In a module

Dim IDValue AS Integer

Sub setIDValue(id As Integer) 
    IDValue = id
End Sub

Function getIDValue() As Integer
    getIDValue = IDValue
End Function

Then from any of your code

'This will set you ID
setIDValue YOUR_VALUE
'This will retrieve the value
getIDValue
'so your code could be 
setIDValue Me.Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT * FROM Companies WHERE Companies.CompanyID = " & getIDValue

Obviously this could use some Error Handling in the event that No IDValue is set. Also I used integer even though I see you are using a String the data type should be the same type as CompanyID so you can change Integer to String if needed but your will also have to change your query to SELECT * FROM Companies WHERE Companies.CompanyID = '" & getIDValue & "' because your query implies a number currently.

Sign up to request clarification or add additional context in comments.

Comments

1

You can gain access to another forms controls by specifying the full path. Example :

Forms!MyFormsName!MyControlsName.Value 

and

Forms!frmCustomer!CboCustomer.Column(0)

So if you want to access a value in VBA that is contained in another form and use it as part of a query it would look like:

CompDetailSQL = "SELECT * " & _
                "FROM Companies " & _
                "WHERE CompanyID = " & Forms!frmCustomer!lstBoxCompanyName.Value

1 Comment

Works a treat! Thanks
0

in ms Access you can also simply use form_NAMEofFORM.NAMEofCONTROL

Example you can reference

         dim myAmount as double
         myAmount = form_receipt.total

to access the value showing on form receipt textbox total.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.