Solved

Return only characters left of first space

Posted on 2011-03-01
16
456 Views
Last Modified: 2013-11-27
I have been all over EE to try to figure this out and I can't. I know this is super easy to do so I apologize for even asking.

Basically, I have a scale hooked up to a com port; you push the print button on the scale and, with the help of WinWedge, it puts whatever the scale reads into whatever active application you want. Problem is that it sends too much info from the scale. I get a string of  "1.034 lb g" as my string. How do I do a Trim(Left) or whatever to return just the weight portion? Since I do a calcuation with the weight I need to lop off the text.

Sorry I have no example to start with as I have no idea what I am doing. Also, if there is a 'good' website that can instruct me on this I would appreciate the link. I have EE'ed and googled extensively and can't find the exact answer. Thanks for any input you can give me.
0
Comment
Question by:G Scott
  • 7
  • 4
  • 2
  • +3
16 Comments
 
LVL 28

Assisted Solution

by:omgang
omgang earned 62 total points
ID: 35009732
Left("1.034 lb g", InStr("1.034 lb g", " ")) will return "1.034"

To convery to number you can
CDbl(Left("1.034 lb g", InStr("1.034 lb g", " "))) = 1.034

OM Gang
0
 
LVL 28

Expert Comment

by:omgang
ID: 35009751
In Access VBA Help search for
String functions
quite a bit of information there.
OM Gang
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 35009764
Mystring = left(string, instr(string," "))

Instr tells you where in the string the first instance of the following string appears.
0
 
LVL 28

Expert Comment

by:omgang
ID: 35009772
Actually, I left a space in there.  Should be
Left("1.034 lb g", InStr("1.034 lb g", " ") - 1) will return "1.034"

OM Gang
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 35009774
if you can store this value as a string, or in a field, you can parse the part to the left of the first space using the Left( ) function, like:

strValue = "1.034 lb g"
Left(strValue, instr(strValue, " ") - 1)

The problem with this is that if there is not space, then instr(strValue, " ") will return 0, and you cannot take the left -1 (0-1) characters of strValue.

another way to approach it is with the Split function which parses a string based on a delimeter, in this case the space, and returns an array.  

varArray = split(strArray, " ")

But you don't have to actually have to save the entire array, you can refer to an element of the array like:

?split(strValue, " ")(0)

Hope this helps
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 35009799
You can use

SPLIT("1.034 lb g")(0)

0
 
LVL 1

Author Comment

by:G Scott
ID: 35009906
Ok, when I do this:

Dim myString As String
myString = Left("1034 lb f", InStr("1034 lb f", " ") - 1)
MsgBox myString

Open in new window


the MsgBox says "1034". Just like I want. However, when I do this:

 
Dim myString As String
myString = Left(Me.Text0, InStr(Me.Text0, " ") - 1)
MsgBox myString

Open in new window



I even tried this to no avail:

 
Dim myString As String
myString = Left(" & Me.text0 & ", InStr(" & Me.text0 & ", " ") - 1)

MsgBox myString

Open in new window

0
 
LVL 28

Expert Comment

by:omgang
ID: 35009937
What happens when you
Debug.Print Me.Text0
???
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 28

Expert Comment

by:omgang
ID: 35009944
Or
MsgBox Me.Text0
OM Gang
0
 
LVL 1

Author Comment

by:G Scott
ID: 35009947
Ok. so it appears that the character between the weight and the "lb" is not a space. When I changed the code to:

myString = Left(" & Me.text0 & ", InStr(" & Me.text0 & ", ".") - 1)

It returned a "2", So I changed it to:

myString = Left(" & Me.text0 & ", InStr(" & Me.text0 & ", "l") - 1)

and it worked.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 35009950
Dim myString As String
myString = SPLIT(me.Text0)(0)
MsgBox myString
0
 
LVL 1

Author Comment

by:G Scott
ID: 35009978
Here is the text directly from the printer:

    2.240 lb  G

How can I tell that there is no space character in there?
0
 
LVL 28

Expert Comment

by:omgang
ID: 35010174
Dim strASCII As String
Dim i As Integer

For i = 1 to Len(Me.Text0)
    strASCII = strASCII & Asc(Mid(Me.Text0, i, 1)) & ";"
Next

MsgBox strASCII


This will display a semicolon seperated list of the ASCII character codes for the string
OM Gang
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 63 total points
ID: 35010211
try using the val() function if you are just after the numeric values

val("1.034 lb g")  > 1.034

val(" 1.034 lb g")  > 1.034
0
 
LVL 28

Expert Comment

by:omgang
ID: 35010253
<<try using the val() function if you are just after the numeric values>>

Nahh, that's too easy; not enough lines of code............
How come I didn't think of that?
OM Gang
0
 
LVL 1

Author Closing Comment

by:G Scott
ID: 35010545
Thanks for your help everyone!! It's working great.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

867 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

19 Experts available now in Live!

Get 1:1 Help Now