• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

calling sub in access vba

I have some code that works but is cumbersome to use.

with z as a variant I made calls like this:

z = Decode("Abi", "polo")
z = Decode("Abi", "xc70")
z = Decode("Abi", "208")

It would pass the values and do its job.

I didnt want to hand write every call so now I have a table of clients and a table of vehicles "to do"

I wrote this to create the string based on vehicles that need doing:

Public Function MakeStrCall(Str1 As String, Str2 As String, intType As Integer, Optional Str3 As String) As String
Dim StrOut As String
Dim DecodeStrFront As String

Dim StrEnd As String

Dim CleanStrFront As String
Dim StrComma As String

StrComma = ","
StrEnd = ")"

If intType = 1 Then
    CleanStrFront = "BtnPrepareData_Click("
    StrOut = CleanStrFront & Str1 & StrComma & Str2 & StrComma & Str3 & StrEnd


    DecodeStrFront = "Decode('"
    StrOut = DecodeStrFront & Str1 & "'" & StrComma & "'" & Str2 & "'" & StrEnd
End If

MakeStrCall = StrOut

End Function
Private Sub CmdDecode_Click()
Dim Db As DAO.Database
Set Db = CurrentDb()
Dim rstRangetoProcess As DAO.Recordset

Dim strSQL As String
DoCmd.SetWarnings False
Dim z As Variant

strSQL = "SELECT TblClients.ClientName, TblWishlist.RangeToProcess, TblWishlist.Marque" _
& " FROM TblClients, TblWishlist" _
& " GROUP BY TblClients.ClientName, TblWishlist.RangeToProcess, TblWishlist.Marque;"

Set rstRangetoProcess = Db.OpenRecordset(strSQL)
Dim StrFunctionString As String

With rstRangetoProcess

    countRecords = .RecordCount

    'cycle through the recordset
    Do Until rstRangetoProcess.EOF
        StrFunctionString = MakeStrCall(.Fields("ClientName").Value, .Fields("RangeToProcess").Value, 2)
       z = StrFunctionString
     Debug.Print z

End With

'old way
'z = Decode("Abi", "polo")
'z = Decode("Abi", "xc70")
'z = Decode("Abi", "208")

Open in new window

an example of its output: Decode('Abi','208')

if i now do
 z = StrFunctionString  it doesnt work anymore, so would like advice on how to achieve.
  • 2
1 Solution
Gustav BrockCIOCommented:
You can't. But you could do:

Do Until rstRangetoProcess.EOF
    z = Decode(.Fields("ClientName").Value, .Fields("RangeToProcess").Value)

PeterBaileyUkAuthor Commented:
Gustav BrockCIOCommented:
You are welcome!


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now