MS Access VBA: Split string into pre-defined width
I have MS Access form where the user pastes a string into a field Vars, and I want to reformat that string into a new field so that (a) it retains whole words, and (b) "fits" within 70 columns.
Specifically, the user will be cutting/pasting variable names from SPSS. So the string will go into the field as whole names---no spaces allowed---with line breaks between each variable. So the first bit of VBA code looks like this:
Vars = Replace(Vars, vbCrLf, " ")
which removes the line breaks. But from there, I'm stumped---ultimately I want the long string that is pasted in the Vars field to be put on consecutive multiple lines that each are no longer than 70 columns.
Any help is appreciated!
vba ms-access
add a comment |
I have MS Access form where the user pastes a string into a field Vars, and I want to reformat that string into a new field so that (a) it retains whole words, and (b) "fits" within 70 columns.
Specifically, the user will be cutting/pasting variable names from SPSS. So the string will go into the field as whole names---no spaces allowed---with line breaks between each variable. So the first bit of VBA code looks like this:
Vars = Replace(Vars, vbCrLf, " ")
which removes the line breaks. But from there, I'm stumped---ultimately I want the long string that is pasted in the Vars field to be put on consecutive multiple lines that each are no longer than 70 columns.
Any help is appreciated!
vba ms-access
2
UseSplit(textHere. vbCrLf)to get an array of variable names, then loop over the array and build up the final string, adding to each line until you hit the 70char limit. Add a new line and continue when that happens.
– Tim Williams
Nov 14 '18 at 23:36
Do note that IBM offers an SPSS ODBC driver, so Access can directly interface with .sav files. Using pasted content and parsing that is both more difficult and more error-prone than just working with the file.
– Erik A
Nov 15 '18 at 8:20
Erik, I've had mixed luck with the ODBC driver. For one thing, I think SPSS v. 25 is 64 bit and if the user has a 32 bit version of MS Access, it doesn't work. Also I'm looking to grab variable names, not their values, for repurposing into syntax. I tried using OMS to push those variable names into Access but it was still just faster to cut/paste!
– DrL
Nov 15 '18 at 17:41
add a comment |
I have MS Access form where the user pastes a string into a field Vars, and I want to reformat that string into a new field so that (a) it retains whole words, and (b) "fits" within 70 columns.
Specifically, the user will be cutting/pasting variable names from SPSS. So the string will go into the field as whole names---no spaces allowed---with line breaks between each variable. So the first bit of VBA code looks like this:
Vars = Replace(Vars, vbCrLf, " ")
which removes the line breaks. But from there, I'm stumped---ultimately I want the long string that is pasted in the Vars field to be put on consecutive multiple lines that each are no longer than 70 columns.
Any help is appreciated!
vba ms-access
I have MS Access form where the user pastes a string into a field Vars, and I want to reformat that string into a new field so that (a) it retains whole words, and (b) "fits" within 70 columns.
Specifically, the user will be cutting/pasting variable names from SPSS. So the string will go into the field as whole names---no spaces allowed---with line breaks between each variable. So the first bit of VBA code looks like this:
Vars = Replace(Vars, vbCrLf, " ")
which removes the line breaks. But from there, I'm stumped---ultimately I want the long string that is pasted in the Vars field to be put on consecutive multiple lines that each are no longer than 70 columns.
Any help is appreciated!
vba ms-access
vba ms-access
asked Nov 14 '18 at 22:53
DrLDrL
1319
1319
2
UseSplit(textHere. vbCrLf)to get an array of variable names, then loop over the array and build up the final string, adding to each line until you hit the 70char limit. Add a new line and continue when that happens.
– Tim Williams
Nov 14 '18 at 23:36
Do note that IBM offers an SPSS ODBC driver, so Access can directly interface with .sav files. Using pasted content and parsing that is both more difficult and more error-prone than just working with the file.
– Erik A
Nov 15 '18 at 8:20
Erik, I've had mixed luck with the ODBC driver. For one thing, I think SPSS v. 25 is 64 bit and if the user has a 32 bit version of MS Access, it doesn't work. Also I'm looking to grab variable names, not their values, for repurposing into syntax. I tried using OMS to push those variable names into Access but it was still just faster to cut/paste!
– DrL
Nov 15 '18 at 17:41
add a comment |
2
UseSplit(textHere. vbCrLf)to get an array of variable names, then loop over the array and build up the final string, adding to each line until you hit the 70char limit. Add a new line and continue when that happens.
– Tim Williams
Nov 14 '18 at 23:36
Do note that IBM offers an SPSS ODBC driver, so Access can directly interface with .sav files. Using pasted content and parsing that is both more difficult and more error-prone than just working with the file.
– Erik A
Nov 15 '18 at 8:20
Erik, I've had mixed luck with the ODBC driver. For one thing, I think SPSS v. 25 is 64 bit and if the user has a 32 bit version of MS Access, it doesn't work. Also I'm looking to grab variable names, not their values, for repurposing into syntax. I tried using OMS to push those variable names into Access but it was still just faster to cut/paste!
– DrL
Nov 15 '18 at 17:41
2
2
Use
Split(textHere. vbCrLf) to get an array of variable names, then loop over the array and build up the final string, adding to each line until you hit the 70char limit. Add a new line and continue when that happens.– Tim Williams
Nov 14 '18 at 23:36
Use
Split(textHere. vbCrLf) to get an array of variable names, then loop over the array and build up the final string, adding to each line until you hit the 70char limit. Add a new line and continue when that happens.– Tim Williams
Nov 14 '18 at 23:36
Do note that IBM offers an SPSS ODBC driver, so Access can directly interface with .sav files. Using pasted content and parsing that is both more difficult and more error-prone than just working with the file.
– Erik A
Nov 15 '18 at 8:20
Do note that IBM offers an SPSS ODBC driver, so Access can directly interface with .sav files. Using pasted content and parsing that is both more difficult and more error-prone than just working with the file.
– Erik A
Nov 15 '18 at 8:20
Erik, I've had mixed luck with the ODBC driver. For one thing, I think SPSS v. 25 is 64 bit and if the user has a 32 bit version of MS Access, it doesn't work. Also I'm looking to grab variable names, not their values, for repurposing into syntax. I tried using OMS to push those variable names into Access but it was still just faster to cut/paste!
– DrL
Nov 15 '18 at 17:41
Erik, I've had mixed luck with the ODBC driver. For one thing, I think SPSS v. 25 is 64 bit and if the user has a 32 bit version of MS Access, it doesn't work. Also I'm looking to grab variable names, not their values, for repurposing into syntax. I tried using OMS to push those variable names into Access but it was still just faster to cut/paste!
– DrL
Nov 15 '18 at 17:41
add a comment |
1 Answer
1
active
oldest
votes
Okay, for posterity, here is a solution:
The field name on the form that captures the user input is VarList. The call to the SPSS_Syntax function below returns the list of variable names (in "Vars") that can then be used elsewhere:
Vars = SPSS_Syntax(me.VarList)
Recall that user input into Varlist comes in as each variable (word) with a line break in between each. The problem is that we want the list to be on one line (horizontal, not vertical) AND a line can be no more than 256 characters in length (I'm setting it to 70 characters below). Here's the function:
Public Function SPSS_Syntax(InputString As String)
InputString = Replace(InputString, vbNewLine, " ") 'Puts the string into one line, separated by a space.
MyLength = Len(InputString) 'Computes length of the string
If MyLength < 70 Then 'if the string is already short enough, just returns it as is.
SPSS_Syntax = InputString
Exit Function
End If
MyArray = Split(InputString, " ") 'Creates the array
Dim i As Long
For i = LBound(MyArray) To UBound(MyArray) 'for each element in the array
MyString = MyString & " " & MyArray(i) 'combines the string with a blank space in between
If Len(MyString) > 70 Then 'when the string gets to be more than 70 characters
Syntax = Syntax & " " & vbNewLine & MyString 'saves the string as a new line
MyString = "" 'erases string value for next iteration
End If
Next
SPSS_Syntax = Syntax
End Function
There's probably a better way to do it but this works. Cheers.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309935%2fms-access-vba-split-string-into-pre-defined-width%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Okay, for posterity, here is a solution:
The field name on the form that captures the user input is VarList. The call to the SPSS_Syntax function below returns the list of variable names (in "Vars") that can then be used elsewhere:
Vars = SPSS_Syntax(me.VarList)
Recall that user input into Varlist comes in as each variable (word) with a line break in between each. The problem is that we want the list to be on one line (horizontal, not vertical) AND a line can be no more than 256 characters in length (I'm setting it to 70 characters below). Here's the function:
Public Function SPSS_Syntax(InputString As String)
InputString = Replace(InputString, vbNewLine, " ") 'Puts the string into one line, separated by a space.
MyLength = Len(InputString) 'Computes length of the string
If MyLength < 70 Then 'if the string is already short enough, just returns it as is.
SPSS_Syntax = InputString
Exit Function
End If
MyArray = Split(InputString, " ") 'Creates the array
Dim i As Long
For i = LBound(MyArray) To UBound(MyArray) 'for each element in the array
MyString = MyString & " " & MyArray(i) 'combines the string with a blank space in between
If Len(MyString) > 70 Then 'when the string gets to be more than 70 characters
Syntax = Syntax & " " & vbNewLine & MyString 'saves the string as a new line
MyString = "" 'erases string value for next iteration
End If
Next
SPSS_Syntax = Syntax
End Function
There's probably a better way to do it but this works. Cheers.
add a comment |
Okay, for posterity, here is a solution:
The field name on the form that captures the user input is VarList. The call to the SPSS_Syntax function below returns the list of variable names (in "Vars") that can then be used elsewhere:
Vars = SPSS_Syntax(me.VarList)
Recall that user input into Varlist comes in as each variable (word) with a line break in between each. The problem is that we want the list to be on one line (horizontal, not vertical) AND a line can be no more than 256 characters in length (I'm setting it to 70 characters below). Here's the function:
Public Function SPSS_Syntax(InputString As String)
InputString = Replace(InputString, vbNewLine, " ") 'Puts the string into one line, separated by a space.
MyLength = Len(InputString) 'Computes length of the string
If MyLength < 70 Then 'if the string is already short enough, just returns it as is.
SPSS_Syntax = InputString
Exit Function
End If
MyArray = Split(InputString, " ") 'Creates the array
Dim i As Long
For i = LBound(MyArray) To UBound(MyArray) 'for each element in the array
MyString = MyString & " " & MyArray(i) 'combines the string with a blank space in between
If Len(MyString) > 70 Then 'when the string gets to be more than 70 characters
Syntax = Syntax & " " & vbNewLine & MyString 'saves the string as a new line
MyString = "" 'erases string value for next iteration
End If
Next
SPSS_Syntax = Syntax
End Function
There's probably a better way to do it but this works. Cheers.
add a comment |
Okay, for posterity, here is a solution:
The field name on the form that captures the user input is VarList. The call to the SPSS_Syntax function below returns the list of variable names (in "Vars") that can then be used elsewhere:
Vars = SPSS_Syntax(me.VarList)
Recall that user input into Varlist comes in as each variable (word) with a line break in between each. The problem is that we want the list to be on one line (horizontal, not vertical) AND a line can be no more than 256 characters in length (I'm setting it to 70 characters below). Here's the function:
Public Function SPSS_Syntax(InputString As String)
InputString = Replace(InputString, vbNewLine, " ") 'Puts the string into one line, separated by a space.
MyLength = Len(InputString) 'Computes length of the string
If MyLength < 70 Then 'if the string is already short enough, just returns it as is.
SPSS_Syntax = InputString
Exit Function
End If
MyArray = Split(InputString, " ") 'Creates the array
Dim i As Long
For i = LBound(MyArray) To UBound(MyArray) 'for each element in the array
MyString = MyString & " " & MyArray(i) 'combines the string with a blank space in between
If Len(MyString) > 70 Then 'when the string gets to be more than 70 characters
Syntax = Syntax & " " & vbNewLine & MyString 'saves the string as a new line
MyString = "" 'erases string value for next iteration
End If
Next
SPSS_Syntax = Syntax
End Function
There's probably a better way to do it but this works. Cheers.
Okay, for posterity, here is a solution:
The field name on the form that captures the user input is VarList. The call to the SPSS_Syntax function below returns the list of variable names (in "Vars") that can then be used elsewhere:
Vars = SPSS_Syntax(me.VarList)
Recall that user input into Varlist comes in as each variable (word) with a line break in between each. The problem is that we want the list to be on one line (horizontal, not vertical) AND a line can be no more than 256 characters in length (I'm setting it to 70 characters below). Here's the function:
Public Function SPSS_Syntax(InputString As String)
InputString = Replace(InputString, vbNewLine, " ") 'Puts the string into one line, separated by a space.
MyLength = Len(InputString) 'Computes length of the string
If MyLength < 70 Then 'if the string is already short enough, just returns it as is.
SPSS_Syntax = InputString
Exit Function
End If
MyArray = Split(InputString, " ") 'Creates the array
Dim i As Long
For i = LBound(MyArray) To UBound(MyArray) 'for each element in the array
MyString = MyString & " " & MyArray(i) 'combines the string with a blank space in between
If Len(MyString) > 70 Then 'when the string gets to be more than 70 characters
Syntax = Syntax & " " & vbNewLine & MyString 'saves the string as a new line
MyString = "" 'erases string value for next iteration
End If
Next
SPSS_Syntax = Syntax
End Function
There's probably a better way to do it but this works. Cheers.
answered Nov 15 '18 at 17:34
DrLDrL
1319
1319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309935%2fms-access-vba-split-string-into-pre-defined-width%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Use
Split(textHere. vbCrLf)to get an array of variable names, then loop over the array and build up the final string, adding to each line until you hit the 70char limit. Add a new line and continue when that happens.– Tim Williams
Nov 14 '18 at 23:36
Do note that IBM offers an SPSS ODBC driver, so Access can directly interface with .sav files. Using pasted content and parsing that is both more difficult and more error-prone than just working with the file.
– Erik A
Nov 15 '18 at 8:20
Erik, I've had mixed luck with the ODBC driver. For one thing, I think SPSS v. 25 is 64 bit and if the user has a 32 bit version of MS Access, it doesn't work. Also I'm looking to grab variable names, not their values, for repurposing into syntax. I tried using OMS to push those variable names into Access but it was still just faster to cut/paste!
– DrL
Nov 15 '18 at 17:41