calling sub in access vba

Posted on 2014-08-19
Last Modified: 2014-08-19
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.
Question by:PeterBaileyUk
    LVL 48

    Accepted Solution

    You can't. But you could do:

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


    Author Closing Comment

    LVL 48

    Expert Comment

    by:Gustav Brock
    You are welcome!


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now