If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > How to assign single value query to a variable in VBA

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-12, 09:29
Postman Postman is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
How to assign single value query to a variable in VBA

Hello,

I have the following query that return the sum of prices.

"SELECT Sum(PartsList.CustomerPrice)AS Expr1 FROM PartsList INNER JOIN Items_Quote ON PartsList.ItemNumber = Items_Quote.ItemNumber WHERE (((Items_Quote.QuoteNumber)=" & QuoteRef & ")))"

I was not able to use the record set to read the value Expr1 and to assign it to a variable in the code.

Any help will be highly appreciated.
Reply With Quote
  #2 (permalink)  
Old 01-30-12, 10:27
dianacris dianacris is offline
Registered User
 
Join Date: Jan 2012
Posts: 1
Are you sure the recordset returned a record? It looks like you have an extra ")" in the query. What does the rest of your code look like?
Reply With Quote
  #3 (permalink)  
Old 01-30-12, 10:53
Postman Postman is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
It works now

Thanks for your reply. It works now after updating the SQL statement. I found more syntax errors in it. But before the modification I was getting an error that that the object is not supported in the red code below:

Public Sub QuoteValue()
Dim dbs As DAO.Database
Dim SumSQLtext As String
Dim QuoteRef As String
Dim rst As DAO.Recordset
Dim QuoteValue

QuoteRef = Forms("enter items").Controls("text75")
SumSQLtext = "(SELECT Sum(PartsList.CustomerPrice)AS Expr1 FROM PartsList INNER JOIN Items_Quote ON PartsList.ItemNumber = Items_Quote.ItemNumber WHERE ((Items_Quote.QuoteNumber)= '" & QuoteRef & "'))"

Set dbs = CurrentDb

'Get the sum of quotation items

Set rst = dbs.OpenRecordset(SumSQLtext)

Do While Not rst.EOF
QuoteValue = (rst![Expr1])

rst.MoveNext
Loop

rst.Close
Forms("enter items").Controls("text77") = QuoteValue
dbs.Close


Forms![Enter Items].[Query1 subform1].Form.Requery
End Sub
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On