Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2004, 07:08 PM
Registered User
 
Join Date: Jan 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default String Manipulation in VB6

I posed the question below to the Beginning VB forum but did not get a helpful answer. Any assistance from the Pros will be appreciated.

I have an access database that contains alphabets and numbers eg:

HP Monitors: Item code: 1000000001. Location: Main
Epson Printers: Item code: 1000000002: Location: Springfield
Dell Dimension 2400: Item code: 1000000003. Location: Satellite
Brother HL - 5040 Printer: Item code: 1000000004: Location: Colesville

I'm looking for a way to extract just the item codes and transfer them to a bound text box in an Access form with VB code. I'm unable to use the Left, Right or Mid functions because the item codes appear
in random positions. Any suggestions will be greatly appreciated.

Thanks,
Jay


 
Old January 19th, 2004, 07:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

You could use the INSTR function to find the location of the labels in the string and then pass that value to the MID function ? Alternatively it looks like all the field / value pairs are seperated by a :, so I guess you could use the SPLIT command to create an array from them ;)
 
Old January 19th, 2004, 08:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

I would have initially suggested the split, but sometimes there is a ":" after the code, and sometimes there is a ".". The lack of consistency could make the split a pain to use. I would go with InStr for that reason.



John R Lick
[email protected]
 
Old January 20th, 2004, 06:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

seems like a waste of a database if you're going to bung all the data in one field. why not have separate fields for theses things?
 
Old January 20th, 2004, 10:14 AM
Authorized User
 
Join Date: Dec 2003
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I posted a response that will do the trick

Use the instr() function to determine the postion of the 'item code' portion of the string. That function will return an integer which can then be used in the mid string function. Again, if you need help with the particular code feel free to email me

Happy Programming

John

 
Old January 20th, 2004, 12:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

Here are a couple ways to do it, depending on what you know about the item codes.

Code:
Option Explicit

Private Sub Command1_Click()
    Dim strField As String
    Dim lngItemCodePosition As Long

    ' Get field data (I am just using a string assignment for the example).
    strField = "HP Monitors: Item code: 1000000001. Location: Main" & vbCrLf & _
               "Epson Printers: Item code: 1000000002: Location: Springfield" & vbCrLf & _
               "Dell Dimension 2400: Item code: 1000000003. Location: Satellite" & vbCrLf & _
               "Brother HL - 5040 Printer: Item code: 1000000004: Location: Colesville"

    ' Initialize value
    lngItemCodePosition = 0

    ' This assumes that the length of the item code is always 10 digits (as in example).
    Do

        ' Instr will return the "next" position a value occurs at. It will return 0
        ' if the value does not exist (again).
        lngItemCodePosition = InStr((lngItemCodePosition + 1), strField, "Item code:")

        If lngItemCodePosition > 0 Then
            ' This assumes that the length of the item code is always 10 digits (as in example).
            Debug.Print "Item Code: " & Mid$(strField, lngItemCodePosition + 11, 10)
        End If

    Loop Until lngItemCodePosition = 0



    ' Re-Initialize value
    lngItemCodePosition = 1

    ' This assumes that the length of the item code can change, but that:
    ' 1) The item code is numeric (only).
    ' 2) There is a non numeric character after the item code.
    Dim strItemCode As String
    Dim strNextCharacter As String
    Do

        ' Instr will return the "next" position a value occurs at. It will return 0
        ' if the value does not exist (again).
        lngItemCodePosition = InStr((lngItemCodePosition), strField, "Item code:")

        ' Check that another item code existed.
        If lngItemCodePosition > 0 Then

            ' Account for the "Item code: " text.
            lngItemCodePosition = lngItemCodePosition + 11

            ' Initialize values
            strItemCode = ""
            strNextCharacter = Mid$(strField, lngItemCodePosition, 1)

            ' Loop through all the numeric values.
            Do While IsNumeric(strNextCharacter)

                ' Build the item code.
                strItemCode = strItemCode & strNextCharacter

                ' Increment so we can look at the next character.
                lngItemCodePosition = lngItemCodePosition + 1

                ' Get the next character.
                strNextCharacter = Mid$(strField, lngItemCodePosition, 1)

            Loop

            Debug.Print "Item Code: " & strItemCode
        End If

    Loop Until lngItemCodePosition = 0

End Sub
John R Lick
[email protected]
 
Old January 20th, 2004, 12:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

I agree with pgtips, the data base could/should have been used better.

John R Lick
[email protected]
 
Old January 20th, 2004, 01:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I cannot agree more than this.
Writing a lot of code to deal with database is a waste of time and resources. Besides, you have to keep your code updated with the database itself.
The database should expose views/functions to retrieve its data. If this is not possible, it means that the database was poorly designed to start with.
This is just phylosofical, I know very well (been there, done it) that the reality is different :)

Marco

Quote:
quote:Originally posted by jlick
 I agree with pgtips, the data base could/should have been used better.

John R Lick
[email protected]





Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Franco1 Visual Basic 2005 Basics 1 July 16th, 2008 12:53 PM
String manipulation pcase XSLT 5 June 14th, 2007 10:32 AM
String manipulation YoungLuke C# 4 May 4th, 2007 01:46 AM
String Manipulation twsinc Access 3 February 23rd, 2004 09:57 AM
String Manipulation Ben Access VBA 2 July 8th, 2003 05:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.