Setting a variable value using a SELECT query

benkingery

Registered User.
Local time
Yesterday, 18:16
Joined
Jul 15, 2008
Messages
153
I have a variable I need to populate with a value and in turn populate a field on a form with that variable value.

The value I want to set the variable to is the result of this query:
Code:
SELECT TOP 1 WarehouseLocation.LocationCode FROM WarehouseLocation WHERE (WarehouseLocation.CurrentUnits < WarehouseLocation.MaxUnits) AND (LocationCode Like 'LT*') ORDER BY LocationCode

What i currently have is this:

Code:
Dim SelectedLocationSQL As String
Dim SelectedLocation As String

SelectedLocationSQL = "SELECT TOP 1 WarehouseLocation.LocationCode FROM WarehouseLocation WHERE (WarehouseLocation.CurrentUnits < WarehouseLocation.MaxUnits) AND (LocationCode Like 'LT*') ORDER BY LocationCode"
SelectedLocation = CurrentProject.Connection.Execute("SelectedLocationSQL").Fields(0).Value

Me.Location = SelectedLocation

I'm struggling with this. I'm new to this type of coding. Anyone offer some solution?

Thanks in advance.
 
More like:

Code:
  Dim strSQL  As String
  Dim db      As DAO.Database
  Dim rs      As DAO.Recordset

  Set db = CurrentDb()
  
  strSQL = "Your SQL string"
  Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

  Me.Location = rs!LocationCode 
  
  set rs = nothing
  set db = nothing
 
That was it! Thank you. I figured it had something to do with a record set. Whenever I get to these types of things I get out of my league.

Thank you
 
Happy to help!
 

Users who are viewing this thread

Back
Top Bottom