Join 300,404 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,623 people online right now. Registration is fast and FREE... Join Now!
Hi, I still very much a beginner! I have a string and would like to remove the first three letters before it is sent to the Textbox. I have written the following code but do not get the desired effect. "Printout = LTrim(Newdata, 3)" Can someone please put me on the right track. I have also tried mit Len but then the result is only numbers!
Paul OE8PCK
CODE
Option Explicit
Dim Newdata As String 'each imcoming packet is assembled here
Private Sub Form_Load() Form1.Caption = "OE8PCK Digital Wattmeter" With MSComm1 .CommPort = 4 .Handshaking = 2 - comRTS .RThreshold = 1 .RTSEnable = True .Settings = "9600,n,8,1" .SThreshold = 1 .PortOpen = True ' Leave all other settings as default values. End With
Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub
Private Sub MSComm1_OnComm()
Dim InBuff As String
Dim I As Integer 'used to inspect each incoming character Dim theChar As String 'each received character Dim theInfo As String Dim Printout As String
Select Case MSComm1.CommEvent ' Handle each event or error by placing ' code below each case statement. ' Errors Case comEventBreak ' A Break was received. Case comEventCDTO ' CD (RLSD) Timeout. Case comEventCTSTO ' CTS Timeout. Case comEventDSRTO ' DSR Timeout. Case comEventFrame ' Framing Error. Case comEventOverrun ' Data Lost. Case comEventRxOver ' Receive buffer overflow. Case comEventRxParity ' Parity Error. Case comEventTxFull ' Transmit buffer full. Case comEventDCB ' Unexpected error retrieving DCB]
' Events Case comEvCD ' Change in the CD line. Case comEvCTS ' Change in the CTS line. Case comEvDSR ' Change in the DSR line. Case comEvRing ' Change in the Ring Indicator. Case comEvReceive ' Received RThreshold # of chars.
InBuff = MSComm1.Input 'received 1 or more characters
For I = 1 To Len(InBuff) 'examine each received character in sequence
theChar = Mid$(InBuff, I, 1) 'extract the next character
If Asc(theChar) = 13 Then 'Look for CR theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
If theInfo = "I" Then Printout = LTrim(Newdata, 3) InformationDisplay.SelLength = 0 InformationDisplay.SelStart = Len(InformationDisplay.Text) InformationDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay InformationDisplay.SelLength = 0
ElseIf theInfo = "P" Then Printout = LTrim(Newdata, 3) OutputDisplay.SelLength = 0 OutputDisplay.SelStart = Len(OutputDisplay.Text) OutputDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay OutputDisplay.SelLength = 0
End If
Newdata = "" 'clear NewData so it can assemble the next packet
ElseIf Asc(theChar) <> 10 Then 'ignore linefeeds
Newdata = Newdata + theChar 'received a character -- append it to NewData
End If
Next I
Case comEvSend ' There are SThreshold number of ' characters in the transmit buffer. Case comEvEOF ' An EOF character was found in the ' input stream. End Select
Is this correct when the string Printout has to be printed. If so it does not work. Sorry
If theInfo = "I" Then Printout.Text = Mid(Newdata, 3) InformationDisplay.SelLength = 0 InformationDisplay.SelStart = Len(InformationDisplay.Text) InformationDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay InformationDisplay.SelLength = 0
ElseIf theInfo = "P" Then Printout.Text = Mid(Newdata, 3) OutputDisplay.SelLength = 0 OutputDisplay.SelStart = Len(OutputDisplay.Text) OutputDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay OutputDisplay.SelLength = 0
dim x as Long dim str1 as string = "killah boy" x=mid(str1,1)
Will generate an error because you're trying to assign a string value to an integer variable. Mid will return the text within a certain range (IndexOf returns an integer value of where the specified character or string was found)
hi to vb5prgrmr no offense but i was curious about your solution so i tried it and i was having a wrong answer i think that the error was the "3" so i changed it to "4"
Hi, I still very much a beginner! I have a string and would like to remove the first three letters before it is sent to the Textbox. I have written the following code but do not get the desired effect. "Printout = LTrim(Newdata, 3)" Can someone please put me on the right track. I have also tried mit Len but then the result is only numbers!
Paul OE8PCK
CODE
Option Explicit
Dim Newdata As String 'each imcoming packet is assembled here
Private Sub Form_Load() Form1.Caption = "OE8PCK Digital Wattmeter" With MSComm1 .CommPort = 4 .Handshaking = 2 - comRTS .RThreshold = 1 .RTSEnable = True .Settings = "9600,n,8,1" .SThreshold = 1 .PortOpen = True ' Leave all other settings as default values. End With
Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub
Private Sub MSComm1_OnComm()
Dim InBuff As String
Dim I As Integer 'used to inspect each incoming character Dim theChar As String 'each received character Dim theInfo As String Dim Printout As String
Select Case MSComm1.CommEvent ' Handle each event or error by placing ' code below each case statement. ' Errors Case comEventBreak ' A Break was received. Case comEventCDTO ' CD (RLSD) Timeout. Case comEventCTSTO ' CTS Timeout. Case comEventDSRTO ' DSR Timeout. Case comEventFrame ' Framing Error. Case comEventOverrun ' Data Lost. Case comEventRxOver ' Receive buffer overflow. Case comEventRxParity ' Parity Error. Case comEventTxFull ' Transmit buffer full. Case comEventDCB ' Unexpected error retrieving DCB]
' Events Case comEvCD ' Change in the CD line. Case comEvCTS ' Change in the CTS line. Case comEvDSR ' Change in the DSR line. Case comEvRing ' Change in the Ring Indicator. Case comEvReceive ' Received RThreshold # of chars.
InBuff = MSComm1.Input 'received 1 or more characters
For I = 1 To Len(InBuff) 'examine each received character in sequence
theChar = Mid$(InBuff, I, 1) 'extract the next character
If Asc(theChar) = 13 Then 'Look for CR theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
If theInfo = "I" Then Printout = LTrim(Newdata, 3) InformationDisplay.SelLength = 0 InformationDisplay.SelStart = Len(InformationDisplay.Text) InformationDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay InformationDisplay.SelLength = 0
ElseIf theInfo = "P" Then Printout = LTrim(Newdata, 3) OutputDisplay.SelLength = 0 OutputDisplay.SelStart = Len(OutputDisplay.Text) OutputDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay OutputDisplay.SelLength = 0
End If
Newdata = "" 'clear NewData so it can assemble the next packet
ElseIf Asc(theChar) <> 10 Then 'ignore linefeeds
Newdata = Newdata + theChar 'received a character -- append it to NewData
End If
Next I
Case comEvSend ' There are SThreshold number of ' characters in the transmit buffer. Case comEvEOF ' An EOF character was found in the ' input stream. End Select
End Sub
QUOTE(Caranzo @ 3 Jul, 2009 - 01:56 AM)
QUOTE(oe8pck @ 2 Jul, 2009 - 05:50 AM)
Hi, I still very much a beginner! I have a string and would like to remove the first three letters before it is sent to the Textbox. I have written the following code but do not get the desired effect. "Printout = LTrim(Newdata, 3)" Can someone please put me on the right track. I have also tried mit Len but then the result is only numbers!
Paul OE8PCK
CODE
Option Explicit
Dim Newdata As String 'each imcoming packet is assembled here
Private Sub Form_Load() Form1.Caption = "OE8PCK Digital Wattmeter" With MSComm1 .CommPort = 4 .Handshaking = 2 - comRTS .RThreshold = 1 .RTSEnable = True .Settings = "9600,n,8,1" .SThreshold = 1 .PortOpen = True ' Leave all other settings as default values. End With
Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub
Private Sub MSComm1_OnComm()
Dim InBuff As String
Dim I As Integer 'used to inspect each incoming character Dim theChar As String 'each received character Dim theInfo As String Dim Printout As String
Select Case MSComm1.CommEvent ' Handle each event or error by placing ' code below each case statement. ' Errors Case comEventBreak ' A Break was received. Case comEventCDTO ' CD (RLSD) Timeout. Case comEventCTSTO ' CTS Timeout. Case comEventDSRTO ' DSR Timeout. Case comEventFrame ' Framing Error. Case comEventOverrun ' Data Lost. Case comEventRxOver ' Receive buffer overflow. Case comEventRxParity ' Parity Error. Case comEventTxFull ' Transmit buffer full. Case comEventDCB ' Unexpected error retrieving DCB]
' Events Case comEvCD ' Change in the CD line. Case comEvCTS ' Change in the CTS line. Case comEvDSR ' Change in the DSR line. Case comEvRing ' Change in the Ring Indicator. Case comEvReceive ' Received RThreshold # of chars.
InBuff = MSComm1.Input 'received 1 or more characters
For I = 1 To Len(InBuff) 'examine each received character in sequence
theChar = Mid$(InBuff, I, 1) 'extract the next character
If Asc(theChar) = 13 Then 'Look for CR theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
If theInfo = "I" Then Printout = LTrim(Newdata, 3) InformationDisplay.SelLength = 0 InformationDisplay.SelStart = Len(InformationDisplay.Text) InformationDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay InformationDisplay.SelLength = 0
ElseIf theInfo = "P" Then Printout = LTrim(Newdata, 3) OutputDisplay.SelLength = 0 OutputDisplay.SelStart = Len(OutputDisplay.Text) OutputDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay OutputDisplay.SelLength = 0
End If
Newdata = "" 'clear NewData so it can assemble the next packet
ElseIf Asc(theChar) <> 10 Then 'ignore linefeeds
Newdata = Newdata + theChar 'received a character -- append it to NewData
End If
Next I
Case comEvSend ' There are SThreshold number of ' characters in the transmit buffer. Case comEvEOF ' An EOF character was found in the ' input stream. End Select
Mid... Returns a Variant (String) containing a specified number of characters from a string.
while Mid$ returns a string.
To see the difference, type in mid and hit the space bar. You should see the tooltop come up. Now backspace and add the $ and then hit space. Now when the tooltip comes up, you should see the As String.
So what does this mean, you ask?
Well when mid returns its variant, vb then needs to coerce the variant into the datatype that is recieving the results of the mid operation which takes longer to do because vb then needs to check the datatype that is recieving this result. Once it knows that, it then changes the result (if possible) to the correct datatype...
CODE
Dim I As Integer
I = 12346
I = Mid(I, 3)
Debug.Print I
while mid$ directly states that it will return a string (even though it will work with above example) and is thus faster when doing string manipulations.
and yeah, go figure, want to remove 3 characters so when typing out mid("strText",3)