User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 3 Nov 2004, 18:10   #1
Supernova9
m u p p e t
 
Join Date: May 2001
Location: Whenever Wherever
Posts: 477
Supernova9 is infamous around these parts
VBA in Access Question

Ok, I want to create a function, called CountRows. (It's going to work like mysql_num_rows, but VBA has nothing to do that ) I'm new to working in VBA, so I can conceptualise things, it's just syntax/getting it into what VBA wants is the difficulty.

The function will be called with strPurchaseOrderRef, a string variable that contains an order number.

I want the function to take that variable, go to the table in my database called tblHWPurchasing, and count how many records have that order number in the field called MasterOrderRef. Then pass back an integer value equalling the number of times that row occurs.

i'm guessing it would involve an SQL count query, but I can't seem to make it work.

Can anyone help?
__________________
Supernova9 is offline   Reply With Quote
Unread 3 Nov 2004, 23:53   #2
Caesar2
Commander
 
Caesar2's Avatar
 
Join Date: Sep 2001
Location: Netherlands
Posts: 146
Caesar2 is just really niceCaesar2 is just really niceCaesar2 is just really niceCaesar2 is just really nice
Re: VBA in Access Question

The SQL you need:

Code:
SELECT COUNT(*) FROM tblHWPurchasing WHERE MasterOrderRef = ordernumber

The function:

Code:
Function strPurchaseOrderRef(Ordernumber As Integer) As Integer
  Dim rs As ADODB.Recordset
  Dim conn As ADODB.Connection
  Set conn = CurrentProject.Connection
  Set rs = New ADODB.Recordset
  rs.Open "SELECT COUNT(*) FROM tblHWPurchasing WHERE MasterOrderRef = " & Ordernumber, conn, adOpenForwardOnly, adLockReadOnly
  strPurchaseOrderRef = rs(0)
  rs.Close
  conn.Close
  Set rs = Nothing
  Set conn = Nothing
End Function
Don't forget to add a reference to your ADO library (assuming you want to use ADO). Tools --> References, check Microsoft ActiveX Data Objects 2.x Library
__________________
Quote:
Originally posted by Cochese
Cathaar are not overpowered.

You were just "bashed", live with it.
Caesar2 is offline   Reply With Quote
Unread 4 Nov 2004, 11:04   #3
Supernova9
m u p p e t
 
Join Date: May 2001
Location: Whenever Wherever
Posts: 477
Supernova9 is infamous around these parts
Re: VBA in Access Question

Ok, that still doesn't work, the debugger takes me to that rs.Open line, and tells me I'm missing a required parameter.

I've got the exact line you specified, so what am I missing? Can anyone else help?
__________________
Supernova9 is offline   Reply With Quote
Unread 4 Nov 2004, 17:27   #4
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: VBA in Access Question

Quote:
Originally Posted by Supernova9
I've got the exact line you specified
The Ado.Connection object as second parameter of open. Or was it the first? Either way, you need to give the connection object.
__________________
"Yay"
Structural Integrity is offline   Reply With Quote
Unread 4 Nov 2004, 19:47   #5
Caesar2
Commander
 
Caesar2's Avatar
 
Join Date: Sep 2001
Location: Netherlands
Posts: 146
Caesar2 is just really niceCaesar2 is just really niceCaesar2 is just really niceCaesar2 is just really nice
Re: VBA in Access Question

You need at least a Source and an ActiveConnection as parameter.
Paste your code if it still doesn't work
__________________
Quote:
Originally posted by Cochese
Cathaar are not overpowered.

You were just "bashed", live with it.
Caesar2 is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 13:15.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018