1

tl;dr
Skip to PROBLEM below.

CONTEXT

In my algebraic math program a formula can be entered in the following forms (as lines of VB6 code). (Spaces within strings will be ignored):

formula1$ = "SUM (a,b,c)"
formula2$ = "SUM (x,y,z)"
formula3$ = "PRODUCT (a,x)"
formula4$ = "PRODUCT ((SUM (a,b,c)) , (SUM (x,y,z))) "
formula5$ = "PRODUCT (" + formula1$ +") , (" + formula2$ +")"

(a bit fiddly for the user, I know, but better on balance when dealing with long formulae).

The content of formula5$ is then identical to the content of formula4$.

It is useful to have a variable formula5_descr$ which when inspected/printed describes the specification of formula5$ thus:

debug.print formula5_descr$ ' --> "PRODUCT (" + formula1$ +") , (" + formula1$ +")"

The code for formula5_descr$ can be produced manually. However it is difficult to do and the user cannot be expected to do it.

I want to let the user enter a simpler form of code like:

formula5$ = "PRODUCT (formula1$ ) , ( formula2$ )"

Then the program could be modified to parse the formula string "PRODUCT (formula1$ ) , ( formula2$ )"and replace any string references (i.e. formula1$ and formula2$) by the contents of the relevant strings (i.e. SUM (a,b,c) and SUM (x,y,z)).

PROBLEM

And here is the problem - how in VB6 to replace the name of a string variable inside a string by the contents of that string variable?

(User Joel Coehoorn has pointed out that a process for this called string interpolation is available in newer languages, like VB14+, C#, javascript.)

Ideally there would be a function REVEAL() such that:

REVEAL("formula1$") --> "SUM (a,b,c)"

NOTE The fact that a solution exists for VBA in MS Access does not mean that the solution automatically works for VB6. They are similar but distinct languages and an installation of VB6 will not necessarily have the same components installed as for MS Access.

16
  • 1
    You could leverage CallByName to implement the REVEAL method, although it would require implementing the variables as Properties. This approach could also be used after you parse out the embedded formulas in formula5. Commented Oct 17, 2023 at 16:01
  • The 2 methods (using Collection and Script Dictionary) described for VBA in the linked question also seem to work in VB6. Commented Oct 17, 2023 at 16:30
  • 1
    They would be properties of the module, but CallByName objects must be in a form or class. So you would need to do a little restructuring of your app to make it work. Commented Oct 17, 2023 at 17:12
  • 1
    I haven't coded in VB6 in a very long time, but here is a "close" question using CallByName() in Excel. Commented Oct 18, 2023 at 2:07
  • 1
    @GSerg If they are defined at run-time, then CallByName() will have zero use. Totally agree with you. It is still unclear to me, however, even after re-reading the question and comments several times over, if the "user" in this case is entering their "code" at run-time in a Form somehow, or if they are actually modifying source code. Commented Nov 10, 2023 at 16:44

2 Answers 2

0

Using a dictionary to store your formulas by key. After that just replace the formula names by the value of them.

Option Explicit

Dim dictFormulas As New Dictionary

Private Sub Command1_Click()
    ReadLines Text1.Text
    PrintFormulas
End Sub

Private Sub ReadLines(strText As Variant)
    Dim i           As Long
    Dim strLines()  As String
    strLines = Split(strText, vbNewLine)
    For i = LBound(strLines) To UBound(strLines)
        AddFormula strLines(i)
    Next
End Sub

Private Sub AddFormula(strLine As String)
    Dim strFormulaName      As String
    Dim strFormulaValue     As String
    Dim strItem             As Variant
    Dim strFormulaVect()    As String
    
    strFormulaVect = Split(strLine, "=")
    strFormulaName = Trim(strFormulaVect(0))
    strFormulaValue = Trim(strFormulaVect(1))
    
    For Each strItem In dictFormulas.Keys
        strFormulaValue = Replace(strFormulaValue, strItem, CStr(dictFormulas(strItem)))
    Next
    
    dictFormulas.Add strFormulaName, strFormulaValue
End Sub

Private Sub PrintFormulas()
    Dim strItem             As Variant
    For Each strItem In dictFormulas.Keys
         Debug.Print strItem & "=" & CStr(dictFormulas(strItem))
    Next
End Sub

Dictionary is part of Microsoft Scripting Library (SCRRUN.DLL) must be added as reference in Project > References

Sign up to request clarification or add additional context in comments.

3 Comments

In my VB6 the first line gives Compile error: User-defined type not defined. A working alternative is: Set dictFormulas = CreateObject("Scripting.Dictionary").
Took me a while understand the logic but now I see that at the end it should loop back to read the next line. This is a good solution to the problem :)
Sorry for the error Compile error: User-defined type not defined. Dictionary is part of Microsoft Scripting Library (SCRRUN.DLL) must be selected in Project > References to be used without CreateObject.
0

What you want to do is called string interpolation, and vb6 is old enough is does not have any support for the feature. You'll need to do something else, like use a dictionary to map a key like formula1$ to a value like SUM (a,b,c), and then do manual replacements of keys to values in the final string.

1 Comment

Thanks for informing me about the name "string interpolation". I see that it is available in VB14+. User jacsetta has already provided an answer using Dictionary and I have accepted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.