Creating variables out by splitting a string (1 Viewer)

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
If I have a string like this:

Code:
strDSH="FT:=Test1,RT:=Test2,ST:=Test3"

How I can create the following variables or controls:

Code:
FT="Test1"
RT="Test2"
ST="Test3"

String length would be increased in different situation. So using split and loops is much preferable.

Thank you.
 

Minty

AWF VIP
Local time
Today, 12:36
Joined
Jul 26, 2013
Messages
10,371
The following will use the pipe character | as your delimiter as it's rarely used in strings.

Code:
Public Function ParseText(TextIn As String, x) As Variant

    On Error Resume Next
    Dim var As Variant
    'Debug.Print TextIn

    var = Split(TextIn, "|", -1)

    ParseText = var(x)

End Function

You simply use it as ParseText(YourStringVariable,1) would return the second element - Element numbering starts at 0
Edit - Actually re-reading your post would need this modifying to give you the exact result you are after...
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
The following will use the pipe character | as your delimiter as it's rarely used in strings.

Code:
Public Function ParseText(TextIn As String, x) As Variant

    On Error Resume Next
    Dim var As Variant
    'Debug.Print TextIn

    var = Split(TextIn, "|", -1)

    ParseText = var(x)

End Function

You simply use it as ParseText(YourStringVariable,1) would return the second element - Element numbering starts at 0
Edit - Actually re-reading your post would need this modifying to give you the exact result you are after...


Thanks for trying to help, but I'm sitting here thinking hard how does it work. As far as I see it's just splitting the string and creates the array.

How my variables names are made?
 

Minty

AWF VIP
Local time
Today, 12:36
Joined
Jul 26, 2013
Messages
10,371
If you want to use it to create variable that will take some modification.
Can you explain the need / requirements for this?

Having "Variable Variables" would tend to indicate possibly a poor data design?
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
If you want to use it to create variable that will take some modification.
Can you explain the need / requirements for this?

Having "Variable Variables" would tend to indicate possibly a poor data design?

It depends on the vision what a poor design means.
Most programming languages are able to create a variable out of a string.
For example in PHP if you have this string:

$myString="TS";

you can convert TS into a variable and give it a value:

$$myString=1

This way you have a new variable called $TS and it's equal to 1.

In php,Python, etc....it's very common.

In this way with a simple loop and an array, you can create numerous variables without going through defining each one of them and give them a value.

In PHP this is very helpful with mass reading data of MySql Databases.
 
Last edited:

Minty

AWF VIP
Local time
Today, 12:36
Joined
Jul 26, 2013
Messages
10,371
It's not common in VBA, Variables are normally (preferably) explicitly declared along with their datatype.
You could use an array to do this, but I don't see the advantage.

Perhaps you could take a step back and describe the data set and what you are trying to achieve. Often a set based solution is far more efficient, if you already have the data available in a sensible fashion? At the moment I'm guessing completely as to your end goal.
 

Mark_

Longboard on the internet
Local time
Today, 04:36
Joined
Sep 12, 2017
Messages
2,111
What do you wish to use the variables for? Or are you trying to update the values inside the string (or a copy of the string) to have them returned?

Your initial string looks like something I'd expect to be passed along with a URL, not something that you would normally run into when importing/exporting data.
 

Mark_

Longboard on the internet
Local time
Today, 04:36
Joined
Sep 12, 2017
Messages
2,111
What do you wish to use the variables for? Or are you trying to update the values inside the string (or a copy of the string) to have them returned?

Your initial string looks like something I'd expect to be passed along with a URL, not something that you would normally run into when importing/exporting data.

EDIT:
Looks like I'm not the only one wondering what your end result is.
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
EDIT:
Looks like I'm not the only one wondering what your end result is.

Here's my situation:

I open a custom form and the user sets different settings for printing a report.
The settings are set into the form's tag.

example:
frm_ReportSettings.Tag="Printer:Ricoh 3300 Ch|Port=1258|Tray:3|PaperSize:458|PrintCount:2|Orientation:portrait|Color:Mono|Leftmargin:25|TopMargin:50|RightMargin:30|BottomMargin:30"

When the user closes the form, this tag is passed to a function that opens a report and sets the properties and prints it.

In this function, I have to open the report, extract each property and set them.
It takes a long code to extract this string and use it to set the report.

But If I can split the string and set each one to a variable (Just like PHP), I'm able to have variables and their values set easily.

And it gives me all variables like this:
Printer="Ricoh 3300 Ch"
Tray = 3
PaperSize = 458
and so on.

then I’m free to do:

with application.Printer
.bin=Tray
.Orientation= Orientation
.ColorMode=Color
......
......
end with


I'm typing from my phone and there may be some syntax typing. I Just want to show the idea.

Please don't ask why instead of reading a string, I don't leave the setting form open and read the settings straight from the form.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:36
Joined
May 21, 2018
Messages
8,527
If you are persist settings for multiple reports, this would be a lot easier to have the form write to a table and each record would be the form and its settinds. If using variables seems a lot easier to have a single variable either a class or a user type. Splitting these out means you have to split the multiple settings into individual settings. Then split them to determine the name of the setting and the value of the setting.

The below will take your string and parse it into a user type "PrinterSettings"

Code:
Public Type PrinterSettings
  PrinterName As String
  Port As Integer
  Tray As Integer
  PaperSize As Integer
  PrintCount As Integer
  Orientation As String
  Color As String
  LeftMargin As Integer
  TopMargin As Integer
  RightMargin As Integer
  BottomMargin As Integer
End Type


Public Function GetPrinterSettings(strSettings As String) As PrinterSettings
  'Assuming all these settings are sent pass in a string and get back a user type
  Dim aSettings() As String
  Dim i As Integer
  aSettings = Split(strSettings, "|")
  For i = 0 To UBound(aSettings)
    'Debug.Print "Printer "; Split(aSettings(0), ":")(1)
    With GetPrinterSettings
       Select Case Split(aSettings(i), ":")(0)
             Case "Printer"
              .PrinterName = Split(aSettings(i), ":")(1)
            Case "Port"
              .Port = CInt(Split(aSettings(i), ":")(1))
            Case "Tray"
              .Tray = CInt(Split(aSettings(i), ":")(1))
            Case "PaperSize"
              .PaperSize = CInt(Split(aSettings(i), ":")(1))
            Case "PrintCount"
              .PrintCount = CInt(Split(aSettings(i), ":")(1))
            Case "Orientation"
               .Orientation = Split(aSettings(i), ":")(1)
            Case "Color"
               .Color = Split(aSettings(i), ":")(1)
            Case "leftMargin"
              .LeftMargin = CInt(Split(aSettings(i), ":")(1))
            Case "RightMargin"
              .RightMargin = CInt(Split(aSettings(i), ":")(1))
            Case "BottomMargin"
              .BottomMargin = CInt(Split(aSettings(i), ":")(1))
       End Select
    End With
  Next i
End Function

Public Sub TestIt()
  Dim strTag As String
  Dim PrinterSettings As PrinterSettings
  strTag = "Printer:Ricoh 3300 Ch|Port:1258|Tray:3|PaperSize:458|PrintCount:2|Orientation:Portrait|Color:Mono|Leftmargin:25|TopMarg in:50|RightMargin:30|BottomMargin:30"
  PrinterSettings = GetPrinterSettings(strTag)
  'demo it works
  Debug.Print PrinterSettings.Orientation
  Debug.Print PrinterSettings.PrinterName
  Debug.Print PrinterSettings.Color
  Debug.Print PrinterSettings.Port
  Debug.Print PrinterSettings.BottomMargin
End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:36
Joined
Feb 28, 2001
Messages
27,172
Again, look at the tail end of the example I posted earlier. It is relevant.
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
Again, look at the tail end of the example I posted earlier. It is relevant.

I appreciate your input and trying to help, but frankly I couldn't understand what you mean.

If you mean it's better to pass or make an array and use it instead of variables, I think it's not an option in my case.
Because we use the same FrontEnd from different PCs and each time the count and options for printing is different. In some cases we don't mind about margin or color or orientation, but in some cases we have to be set them. Thus, each time the count of array is different and it's hard to control what property goes where.

If I try to use an array, in some cases the third member may be color, but in some cases it may be the 5th. So breaking down an array and use it is risky.

If you mean something else, or I'm misunderstanding you, I appreciate any further explanation and am open for any kind of advice/suggestions.

Once again I appreciate your and others input.
 

Mark_

Longboard on the internet
Local time
Today, 04:36
Joined
Sep 12, 2017
Messages
2,111
For myself, I would use a different approach. I would create a table that holds all of the printing parameters for each user for each report. When the report loads, you select the matching entry in the printing parameter table for that report/user and set the report printersettings based off the saved values.

This means you don't have to muck about with arrays and trying to figure out how to pass values when you can make a procedure that is passed report name and user name that does the printer settings for you.
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
If you are persist settings for multiple reports, this would be a lot easier to have the form write to a table and each record would be the form and its settinds. If using variables seems a lot easier to have a single variable either a class or a user type. Splitting these out means you have to split the multiple settings into individual settings. Then split them to determine the name of the setting and the value of the setting.

The below will take your string and parse it into a user type "PrinterSettings"

Code:
Public Type PrinterSettings
  PrinterName As String
  Port As Integer
  Tray As Integer
  PaperSize As Integer
  PrintCount As Integer
  Orientation As String
  Color As String
  LeftMargin As Integer
  TopMargin As Integer
  RightMargin As Integer
  BottomMargin As Integer
End Type


Public Function GetPrinterSettings(strSettings As String) As PrinterSettings
  'Assuming all these settings are sent pass in a string and get back a user type
  Dim aSettings() As String
  Dim i As Integer
  aSettings = Split(strSettings, "|")
  For i = 0 To UBound(aSettings)
    'Debug.Print "Printer "; Split(aSettings(0), ":")(1)
    With GetPrinterSettings
       Select Case Split(aSettings(i), ":")(0)
             Case "Printer"
              .PrinterName = Split(aSettings(i), ":")(1)
            Case "Port"
              .Port = CInt(Split(aSettings(i), ":")(1))
            Case "Tray"
              .Tray = CInt(Split(aSettings(i), ":")(1))
            Case "PaperSize"
              .PaperSize = CInt(Split(aSettings(i), ":")(1))
            Case "PrintCount"
              .PrintCount = CInt(Split(aSettings(i), ":")(1))
            Case "Orientation"
               .Orientation = Split(aSettings(i), ":")(1)
            Case "Color"
               .Color = Split(aSettings(i), ":")(1)
            Case "leftMargin"
              .LeftMargin = CInt(Split(aSettings(i), ":")(1))
            Case "RightMargin"
              .RightMargin = CInt(Split(aSettings(i), ":")(1))
            Case "BottomMargin"
              .BottomMargin = CInt(Split(aSettings(i), ":")(1))
       End Select
    End With
  Next i
End Function

Public Sub TestIt()
  Dim strTag As String
  Dim PrinterSettings As PrinterSettings
  strTag = "Printer:Ricoh 3300 Ch|Port:1258|Tray:3|PaperSize:458|PrintCount:2|Orientation:Portrait|Color:Mono|Leftmargin:25|TopMarg in:50|RightMargin:30|BottomMargin:30"
  PrinterSettings = GetPrinterSettings(strTag)
  'demo it works
  Debug.Print PrinterSettings.Orientation
  Debug.Print PrinterSettings.PrinterName
  Debug.Print PrinterSettings.Color
  Debug.Print PrinterSettings.Port
  Debug.Print PrinterSettings.BottomMargin
End Sub


I had a question here where it was answered. The suggested database was exactly as you explain. The setting was saved into a table and a record source of the table was used to set the printer.

I'm working on a large frontend with 163 forms, 63 reports and 58 linked table to a database on our sql server.
Because of a lot of situations here, I decided not to use a table for settings.

But the idea of user type is excellent. I never thought of that.
I think I will go with this.

Thanks for your time and code.
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
For myself, I would use a different approach. I would create a table that holds all of the printing parameters for each user for each report. When the report loads, you select the matching entry in the printing parameter table for that report/user and set the report printersettings based off the saved values.

This means you don't have to muck about with arrays and trying to figure out how to pass values when you can make a procedure that is passed report name and user name that does the printer settings for you.

Please read post #15 above.
Unfortunately at present using a table is not an option for me.
What you suggest, certainly is a very wise one and the best to go with. But I have to think about two months from now or two years later.

If I save the options for each printer in a table, and read them when printing, you are right. Everything would be fine and done in a blink of an eye, no fuss at all. The user clicks a button, he doesn't see any setting form and the report is printed.
But each time a new printer is added to a machine, or an old printer goes out of service, or someone tries to re-fine printing position on the labels etc, I have to add records to the table, or delete or correct them.


Thanks again for your input.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:36
Joined
May 21, 2018
Messages
8,527
Unfortunately at present using a table is not an option for me.
What you suggest, certainly is a very wise one and the best to go with. But I have to think about two months from now or two years later.

That argument does not seem to make sense. You cannot update the table, but somehow you can update the Tag properties of each form? I cannot see how those two are related. If you were doing this in another coding language you would persist settings to some kind on file, with Access you have the luxury of persisting to a table. Persisting to a tag property of a form seems more problematic.
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
That argument does not seem to make sense. You cannot update the table, but somehow you can update the Tag properties of each form? I cannot see how those two are related. If you were doing this in another coding language you would persist settings to some kind on file, with Access you have the luxury of persisting to a table. Persisting to a tag property of a form seems more problematic.

You're somewhat lost in the middle of what I had explained.
I don't update the tag. The user updates the tag.

When a user needs to print a report, I show him a form that he can set the settings he need, which printer to use, which orientation, what paper size etc.
When the user is OK with what he has selected and clicks print button, these setting is passed to a module by form's tag.
This module opens the report, set it as what the user has selected and prints it.

That's all.

I don't see any privilege in saving these settings in a table and then read them from table.
 

Mark_

Longboard on the internet
Local time
Today, 04:36
Joined
Sep 12, 2017
Messages
2,111
When a user needs to print a report, I show him a form that he can set the settings he need, which printer to use, which orientation, what paper size etc.
When the user is OK with what he has selected and clicks print button, these setting is passed to a module by form's tag.
This module opens the report, set it as what the user has selected and prints it.

That's all.

I don't see any privilege in saving these settings in a table and then read them from table.

If you saved this to a record in a table (what the user wants) then you can reload the saved values the next time the user runs the same report. You don't have to actually do any work adding to or updating the table if your user is already going to be doing this.

The point behind having a table is so a user can enter this information once and use the same values again. As is, they have to enter it each and every time they want to print.
 

deletedT

Guest
Local time
Today, 12:36
Joined
Feb 2, 2019
Messages
1,218
As is, they have to enter it each and every time they want to print.

No, it's not that way.
This application is saving each user's activity.
Any form (except Data Input forms) that opens, reads the activity of the user and determines what he's going to do next. and sets the form accordingly.

In case of printing, when setting form opens, the program recognizes logged in user, searches his activities and opens the form exactly as the last time.
Let's say it is :
Printer 1, ThisReport, Thismargin, ThispaperSize etc
If users changes report name, the form reads the settings the last time was used for that report (from activities table)

I simply don't need another table for printing settings. The form is opened exactly the same as the last time for each user, and they don't change any of these settings except extra cases.

Setting form is only opened for very rare situation that they need to do a refine print result.
Even if I add another table for printing settings, They need to see the setting form for these extra cases.

It's not only about report settings from, but also any other forms such as Material Order, part Order, Product treatment orders.....

Users have 3 kind of options that should be set at first time login.
System setting: The behavior of the application over all.
Machine Settings: This settings is used for when users are logged in with their own client machines.
Personal Settings:It's used when a user logs in to the application from any client in the domain.
 
Last edited:

Users who are viewing this thread

Top Bottom