Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help, look at my problem

Status
Not open for further replies.
May 5, 2000
168
US
I need to revise my code below to search for six digits, an asterisk followed by another digit i.e.,(123456*1). This string only occurs once in the .dat file. I need to replace the "*" with a "-" in the string.

I can't use the code below because it will replace any instances of the "*" when I only want it to replace the string above.


Private Sub CmdData_Click()
Open "a:\test.dat" For Input As #1
Open "a:\testout.dat" For Output As #2

Dim intResponse As Integer

Do Until EOF(1)
Line Input #1, strInput
newstring = Replace(strInput, "*", "-")

Print #2, newstring
Loop

Close #1
Close #2

intResponse = MsgBox("The text has been updated, Exit?", _
vbYesNo + vbQuestion, "Exit Application")
If intResponse = vbYes Then

Unload frmWrite

End If
 
Code:
Public Function basReplace(StrIn As String) As String

    Dim TestStr As String
    Dim ReplStr As String
    Dim tmpStr As String
    Dim RepStrt As Integer

    TestStr = "123456*1"
    ReplStr = "123456-1"

    tmpStr = StrIn
    RepStrt = InStr(StrIn, TestStr)

    If (RepStrt <> 0) Then
        Mid(tmpStr, RepStrt, 8) = ReplStr
    End If

    basReplace = tmpStr

End Function

Look the above code over. It should give enough cluse to do what you want.

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
The string isn't always the same. It is alway six digits, an asterisk followed by one more digit. it could be 999999*9 or 234567*8, etc.

My code worked fine if the string is always the same.

Is there a mask for digits? I tried this:


newstring = Replace(strInput, &quot;######*#&quot;, &quot;######-#&quot;)

It didn't work.

Do you see what I'm getting at

 
A more generalized version:

Code:
Public Function basReplace(StrIn As String, strFind As String, strRepl As String) As String

    Dim tmpStr As String
    Dim RepStrt As Integer

    tmpStr = StrIn
    RepStrt = InStr(StrIn, strFind)

    If (RepStrt <> 0) Then
        tmpStr = Left(StrIn, RepStrt - 1)
        tmpStr = tmpStr &amp; strRepl           'RepStrt, Len(strRepl)) = strRepl
        tmpStr = tmpStr &amp; Right(StrIn, Len(StrIn) - (RepStrt + Len(strFind) - 1))
    End If

    basReplace = tmpStr

End Function

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Try something like this:
Code:
Private Sub Command1_Click()
    Dim sOldStr As String, sNewStr As String
    
    sOldStr = &quot;123456*1&quot;
    sNewStr = sOldStr
    
    'Verify (*) in position 7 and all other chars are numeric
    If InStr(1, sOldStr, &quot;*&quot;) = 7 And _
      IsNumeric(Left(sOldStr, 6)) = True And _
      IsNumeric(Right(sOldStr, 1)) = True Then
            sNewStr = Replace(sOldStr, &quot;*&quot;, &quot;-&quot;)
    End If
    
    MsgBox sNewStr

End Sub
Hope that helps!
 
When I tried your code the new testout.dat file is empty.
The code I tried is below. I Also pasted a typical test.dat file so you can see what the file looks like. The field with the &quot;*&quot; is near the end of the file.

Private Sub CmdData_Click()

Open &quot;c:\vbProjects\a\test.dat&quot; For Input As #1
Open &quot;c:\vbProjects\b\testout.dat&quot; For Output As #2

Dim sOldStr As String, sNewStr As String

sOldStr = &quot;123456*1&quot;
sNewStr = sOldStr

'Verify (*) in position 7 and all other chars are numeric
If InStr(1, sOldStr, &quot;*&quot;) = 7 And _
IsNumeric(Left(sOldStr, 6)) = True And _
IsNumeric(Right(sOldStr, 1)) = True Then
sNewStr = Replace(sOldStr, &quot;*&quot;, &quot;-&quot;)
End If

'MsgBox sNewStr

Print #2, newstring

Close #1
Close #2

Kill &quot;c:\vbProjects\A\test.dat&quot;

test.dat

^job 09700154 -zSACSS2 -aapon -c01
^global EM.01
FOOT DOCTOR OF NOVATO
^global EM.02
^global EM.03
PO BOX 192
^global EM.04
127 N. SAN MATEO DR.
^global EM.05
SAN MATEO
^global EM.06
CA
^global EM.07
94401
^global AP.01
LOPEZ
^global AP.02
DOUG
^global AP.13
111-22-2345
^global CH.NUM
022807*1
^global PW.01
MARK
 
Ooops. I reworded the line

Print #2, newstring to
Print #2, snewstr

Now the testout.dat file has one line and it is


123456-1


 
In your original code:
Code:
    Do Until EOF(1)
        Line Input #1, strInput
        newstring = Replace(strInput, &quot;*&quot;, &quot;-&quot;)

Change the &quot;Replace&quot; in the last line of the above to:
&quot;basReplaceAster&quot;

and copy the below function to your module.


Code:
Public Function basReplAster(StrIn As String) As String

    Dim tmpStr As String
    Dim CurChar As String * 1
    Dim RepStrt As Integer
    Dim Idx As Integer
    Dim NotStrFlg As Boolean

    If (Len(StrIn) <> 8) Then
        GoTo NoRepl
    End If

    For Idx = 1 To 8

        Select Case Idx

            Case 1 To 6, 8
                CurChar = Mid(StrIn, Idx, 1)
                If (Not IsNumeric(CurChar)) Then
                    NotStrFlg = True
                End If

            Case Is = 7
                CurChar = Mid(StrIn, Idx, 1)
                If (CurChar <> &quot;*&quot;) Then
                    NotStrFlg = True
                End If

        End Select
        
    Next Idx
    
    If (NotStrFlg) Then
        GoTo NoRepl
    End If

    tmpStr = StrIn
    Mid(tmpStr, 7, 1) = &quot;-&quot;
    basReplAster = tmpStr
    Exit Function

NoRepl:
    basReplAster = StrIn
    Exit Function

End Function



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
MichaelRed

Tried your code and got a message &quot;Compile Error: byRef argument type mismatch&quot;

in the line newstring = basReplAster(strInput, &quot;*&quot;, &quot;-&quot;) &quot;strInput&quot; is highlighted
 
you can't pass &quot;*&quot; and &quot;-&quot; to basReplAster, it only takes one parameter ray
rheindl@bju.edu

 
janerussel, why don't you do something like this, dsi's suggestion was really good, you just put it in your code wrong. do this instead

Private Sub CmdData_Click()
Open &quot;a:\test.dat&quot; For Input As #1
Open &quot;a:\testout.dat&quot; For Output As #2

Dim intResponse As Integer

Do Until EOF(1)
Line Input #1, strInput
If InStr(1, strInput, &quot;*&quot;) = 7 And IsNumeric(Left(strInput, 6)) And IsNumeric(Right(strInput, 1)) Then
strInput = Replace(strInput, &quot;*&quot;, &quot;-&quot;)
End If

Print #2, strInput
Loop

... rest of function ray
rheindl@bju.edu

 
You need to declare the strInput as String.

Dim strInput as String


Also, as noted by Ray/froggerIII, do not pass the &quot;*&quot; or &quot;-&quot; to the new function.



Do Until EOF(1)
Line Input #1, strInput
newstring = basReplAster(strInput)




MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Thank you so much, it works. I have one more question.

In the source directory c:\vbprojects\a\ there will always be a different number of files and they will all have different names. After the vbprocedure has looked through all the files and removed the &quot;*&quot;, the files have to be copied out to the destination directory c:\vbProjects\b\ with the same names they had in the source directory.


 
For yet another alternative Search pattern look at thread222-48379.

To copy a folder from one place to another, you may want to use the FileSystemObject.


Dim objFso as FileSystemObject
Set objFso = new FileSystemObject

objFso.CopyFolder App.Path &amp; &quot;\FromFolder&quot; , App.Path &amp; &quot;\ToFolder&quot;

Set objFso = Nothing


Replace in your program App.Path &amp; &quot;\FromFolder&quot; with the adequate Source Folder and App.Path &amp; &quot;\ToFolder&quot; with the Destination Folder.

The FileSystemObject is part of the Script Control. It can be added to your project through: Project Menu > References > Scripting Control.

PS. Avoid using meaningless thread titles like : Need Help. If you ask a question, you propably need already help. Isn't it? _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
I viewed the Thread222-48379. Excellent

I know that Need Help isn't a very exciting title, but everytime I type a title relating to my problem, no one seems to read them or my titles just aren't very exciting. I had such an overwhelming response with Need Help, I might have to use something like that again if need be.
 
janerussel,

My pleasure. Conclusion: people are helpfull!

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top