Tired of string concatenation? (1 Viewer)

Tessa

Registered User.
Local time
Today, 01:42
Joined
Nov 15, 2018
Messages
24
If you instead of writing
Code:
filter = "[Name]='" & someName & "' and [Category]='" & someCategory & "' or [Value]='" & someValue & "'"
prefer to write
Code:
filter = Printf("[Name]='%0' AND [Category]='%1' OR [Value]='%2'", someName, someCategory, someValue)
then use this function
Code:
Function Printf(format As String, ParamArray args() As Variant) As String
    Dim i As Integer
    Dim ph As String
    
    Printf = format
    For i = LBound(args) To UBound(args)
        ph = "%" & i
        If InStr(1, format, ph) <> 0 Then
            Printf = Replace(Printf, ph, args(i))
        End If
    Next
End Function
No error handling, I know, but it works fine for me.
Please feel free to comment and suggest improvements.
 

Cronk

Registered User.
Local time
Today, 09:42
Joined
Jul 4, 2013
Messages
2,771
My comments
1. Filter is a reserved word. Use something else
2. There is more typing
3. As is, your function will not handle variables with an apostrophe


But to each her own.
 

Tessa

Registered User.
Local time
Today, 01:42
Joined
Nov 15, 2018
Messages
24
The Printf function in my post is a quick and dirty solution, I know. But I was sick and tired of all those & when concatenating strings. If you have to many strings to concatenate it gets difficult to find out what is going on, at least for me. Printf has made life much easier for me until now.
I have somehow regretted that I published it. Perhaps I were a bit to anxious.
I am working on a new and much more powerful version of Printf which I will not publish before it can handle apostrophe I promise.
 

Cronk

Registered User.
Local time
Today, 09:42
Joined
Jul 4, 2013
Messages
2,771
Tessa, my comments were my view, not in any way critical of you or your efforts. All of the regulars posting here have their own style and methods. I have adopted some methods used by others because I saw them better than the way I had been working, others not because they did not suit me.
 

Dreamweaver

Well-known member
Local time
Today, 00:42
Joined
Nov 28, 2005
Messages
2,466
I always try not to use any words access uses for anything I've found access can be a beast if you give it the smallest thing.
 

Tessa

Registered User.
Local time
Today, 01:42
Joined
Nov 15, 2018
Messages
24
Cronk, I was not offended by your post.

I also adopt style and methods from others and I look forward to explore this site and hopefully learn a lot good stuff about using Access.
 

Users who are viewing this thread

Top Bottom