Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2010, 02:38 PM
Registered User
 
Join Date: May 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default require help with vba query

Hi Guys,

I'm working on a Form in Access 2007. In the vba code window I have the following:

Code:
Private Sub Display_Cost_Centers(SubsectionID As String)

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim query As String
Dim recordCount As Integer

Debug.Print "SubsectionID: " & SubsectionID

query = "SELECT CC_NUMBER FROM COST_CENTER WHERE COST_CENTER.SUBSECTION_ID=" & SubsectionID & ";"
Debug.Print query
'On Error GoTo ErrHandler

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(query, dbOpenSnapshot)

recordCount = rst.recordCount
Debug.Print "RecordCount: " & recordCount

...

END SUB
Please note the query: SELECT CC_NUMBER FROM COST_CENTER WHERE COST_CENTER.SUBSECTION_ID=" & SubsectionID & ";"

As you can see, it is quite a simple statement.
When I create an actual query in Access and use the value one "1" for the SubsectionID, I get the appropriate return of two records from the COST_CENTER table. However, when I run the code above containing the same SQL statement, I get only a return of one record.

Am I missing something? Any ideas as to why the code above is returning only one record when it should be returning two?

Alan
 
Old July 15th, 2010, 02:32 AM
Registered User
 
Join Date: Oct 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Ashier

Looking at your query, it appears that it is 'loosing' the string in subsectionID. try putting ' after the = and before the ; for example ='" & subsectionid & "';".

This should help identify the variable as a string

I have added to the code below.

please advise if works ok

Martin

<CODE>
Private Sub Display_Cost_Centers(SubsectionID As String)

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim query As String
Dim recordCount As Integer

Debug.Print "SubsectionID: " & SubsectionID

query = "SELECT CC_NUMBER FROM COST_CENTER WHERE COST_CENTER.SUBSECTION_ID='" & SubsectionID & "';"
Debug.Print query
'On Error GoTo ErrHandler

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(query, dbOpenSnapshot)

recordCount = rst.recordCount
Debug.Print "RecordCount: " & recordCount

...

END SUB
</CODE>

Last edited by martinrhague; July 15th, 2010 at 02:34 AM.. Reason: Incorrect name





Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Passthrough Query Rood67 Access VBA 13 May 1st, 2012 04:20 PM
Help With SQL Query in VBA Paul_Tic Access VBA 4 May 30th, 2006 06:34 AM
require attribute - all but... kfir XML 2 May 28th, 2006 09:45 AM
VBA Query Problems LiamBFC Access VBA 2 April 11th, 2006 06:26 AM
include() vs require() kc5f Beginning PHP 2 May 23rd, 2004 10:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.