Richard Horne
Member
- Local time
- Today, 06:25
- Joined
- Oct 15, 2020
- Messages
- 55
Morning guys,
This is something of a general VBA/query/structure question as I'm interested to hear how other people approach this issue.
When you're writing a typical VBA sub which requires you to do multiple queries, how do you keep your code well structured, organised, clean and tidy?
Here's what I do and it seems messy to me and I can't help but feel there has to be a better way.
For starters I have a default chunk of connection/query code which I recycle more times than I can remember.
If I need to do additional queries outputting multiple fields, I usually put that in a separate sub (my_separate_sub) to try and keep things organised and readable and to help with troubleshooting. If I'm querying 10+ fields multiple times then the code gets pretty unwieldy, but of course this method means also often having to create public variables in order to be able to reference them across multiple subs.
If I need to do multiple queries within the same sub then I have to declare multiple recordset variables (rs, rs2, rs3), multiple sql strings (str_sql, str_sql2, str_sql3) and then remember to close them all.
Is this an acceptable way or is there a neater more efficient method? Is it inefficient continually opening new recordsets across multiple subs?
For example:
Thanks in advance.
This is something of a general VBA/query/structure question as I'm interested to hear how other people approach this issue.
When you're writing a typical VBA sub which requires you to do multiple queries, how do you keep your code well structured, organised, clean and tidy?
Here's what I do and it seems messy to me and I can't help but feel there has to be a better way.
For starters I have a default chunk of connection/query code which I recycle more times than I can remember.
Code:
'Set our DB connect variables
Dim db As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim str_sql As String
Set db = CurrentProject.Connection
Set rs = New ADODB.Recordset
'Dim the variables we will use to capture the query outputs
Dim str_my_var1 as string, int_my_var2 as integer, dbl_my_var3 as double
'Build our SQL query
str_sql = "SELECT * FROM WHATEVER"
Set rs = db.Execute(str_sql)
'Capture the results and convert to variables
If IsNull(rs(0)) Then
str_my_var1= ""
Else
str_my_var1= rs(0)
End If
If IsNull(rs(1)) Then
int_my_var2= 0
Else
int_my_var2= rs(1)
End If
If IsNull(rs(2)) Then
dbl_my_var3= 0
Else
dbl_my_var3= rs(2)
End If
'Do something with the vars
'Call my separate sub containing further queries or code
Call my_separate_sub
Set rs = Nothing
Set db = Nothing
If I need to do additional queries outputting multiple fields, I usually put that in a separate sub (my_separate_sub) to try and keep things organised and readable and to help with troubleshooting. If I'm querying 10+ fields multiple times then the code gets pretty unwieldy, but of course this method means also often having to create public variables in order to be able to reference them across multiple subs.
If I need to do multiple queries within the same sub then I have to declare multiple recordset variables (rs, rs2, rs3), multiple sql strings (str_sql, str_sql2, str_sql3) and then remember to close them all.
Is this an acceptable way or is there a neater more efficient method? Is it inefficient continually opening new recordsets across multiple subs?
For example:
Code:
'Set our DB connect variables
Dim db As ADODB.Connection
Dim rs As New ADODB.Recordset, rs2 as New ADODB.Recordset, rs3 as New ADODB.recordset
Dim str_sql As String, str_sql2 as string, str_sql3 as string
Set db = CurrentProject.Connection
Set rs = New ADODB.Recordset, rs2 = New ADODB.Recordset, rs3 as New ADODB.recordset
Dim int_my_var as integer, int_my_var2 as integer, int_my_var3 as integer
'Build our SQL query
str_sql = "SELECT"
Set rs = db.Execute(str_sql)
If IsNull(rs(0)) Then
int_my_var= 0
Else
int_my_var= rs(0)
End If
'Build our 2nd SQL query
str_sql2 = "SELECT * FROM OTHER_TABLE WHERE field= " & int_my_var
Set rs2 = db.Execute(str_sql2)
If IsNull(rs2(0)) Then
int_my_var2= 0
Else
int_my_var2= rs2(0)
End If
'Build our 3rd SQL query
str_sql3 = "SELECT * FROM OTHER_TABLE WHERE field= " & int_my_var2
Set rs3 = db.Execute(str_sql3)
If IsNull(rs3(0)) Then
int_my_var3= 0
Else
int_my_var3= rs3(0)
End If
Set rs = Nothing
Set rs2 = Nothing
Set rs3 = Nothing
Set db = Nothing
Thanks in advance.