NotAnExpert
Registered User.
- Local time
- Today, 21:29
- Joined
- Feb 3, 2017
- Messages
- 46
Hi, i'm struggling with using Nz with SQL in VBA, I believe it's the use of " that might be the issue. Would one of you kind fellows be helpful enough to show me where I might be going wrong?
The idea is that there is some data in a single table which isn't relational, but the user wants to be able to search any of the fields on there. This is an example of what is there so far. Text1 has multiple search options on the main form hence the searchArray(). The reason I want to use Nz is to replace null values in the fields with a blank so the row isn't ignored when using the query. I need all rows to show even if the field/column is null or blank.
The idea is that there is some data in a single table which isn't relational, but the user wants to be able to search any of the fields on there. This is an example of what is there so far. Text1 has multiple search options on the main form hence the searchArray(). The reason I want to use Nz is to replace null values in the fields with a blank so the row isn't ignored when using the query. I need all rows to show even if the field/column is null or blank.
Code:
Private Sub CreateStrSQL()
Dim sqlSELECT As String, sqlFROM As String, sqlWHERE As String, sqlORDER As String, strSQL As String, searchArray() As String
Dim i As Integer
'sqlSELECT = "SELECT * " '-- Original catch all - works fine
sqlSELECT = "SELECT Acc, Nz([qryTransactions].[Recnum], "") AS [Recnum], [Cust ref], [Del dt] " '-- "" within Nz not working as expected...
sqlFROM = "FROM qryTransactions "
sqlWHERE = "WHERE [Acc] LIKE '*" & Me.cboCustAccRef & "*' AND [Recnum] LIKE '*" & Me.txtRecNo & "*' AND [Cust ref] LIKE '*" & Me.txtOrdNo & "*'"
sqlORDER = "ORDER BY Recnum DESC"
searchArray() = Split(Me.txtText1.Value & "", ";")
For i = 0 To UBound(searchArray)
sqlWHERE = sqlWHERE & " AND Text1 LIKE '*" & searchArray(i) & "*'"
Next i
strSQL = sqlSELECT & sqlFROM & sqlWHERE & sqlORDER
Me.sfrmTransactions.Form.RecordSource = strSQL
End Sub