?
Solved

Fix it please

Posted on 1998-07-01
11
Medium Priority
?
132 Views
Last Modified: 2010-05-03
I have my progect in a zip file at <A HREF="www.shasta.cc.ca.us/cfdocs/test/myvb.zip"> www.shasta.cc.ca.us/cfdocs/test/myvb.zip</A>. Every time i run it it hangs when it tries to run the szreplace function. I f anyone could make this run again i'll shell ou all of my points (currently 135). It was done in VB4.
 
0
Comment
Question by:hess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 1

Accepted Solution

by:
brunchey earned 520 total points
ID: 1464711
the problem with your function is that it always starts at the beginning of the passed in string...

nThere = InStr(szTemp, szFind)

The actual syntax for InStr(startloc, string1, string2) where startloc is where you want it to start searching in string 1.  If you omit this, it will always start at the first letter in the string.  You need to change this location on every interation of the loop like so

Dim ll_begin as long
ll_begin = 1
Do
     nThere = InStr(ll_begin,szTemp, szFind)

     If nthere > 0 then    'we found one!!!
          szTemp = Left$(szTemp, nThere - 1) & _
                  szRepl & Mid$(szTemp,nThere + Len(szFind))
          ll_begin = nthere + len(szRepl)
     else
          exit do
     end if
loop
     
Loop


0
 
LVL 1

Expert Comment

by:Sekans
ID: 1464712
hess
Do you have the Microsoft DAO Object library loaded?  If not, try that.

Regards,
Sekans
0
 
LVL 1

Expert Comment

by:Sekans
ID: 1464713
brunchey,
I don't think that is the problem.  Every time a match is found, it is replaced with something else.  So even if it starts at the beginning again, it won't find the same occurrance, because it won't be there anymore.

assume temp = "test text"
search for "t" and replace with "X"

first pass starts at first character finds "t" at position 1 changes to X"
now temp = "Xest text"
second pass starts at first character finds "t" at position 4
now temp = "XesX test"

Regards,
Sekans

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:Sekans
ID: 1464714
brunchey,
Please accept my apology.  I failed to check the values of sfind and sreplace.  You are absolutely correct.  Your answer should solve the problem.  Good work.

Regards,
Sekans
0
 
LVL 1

Expert Comment

by:Sekans
ID: 1464715
hess,
You do need to initialize the variable ll_begin.

Dim ll_begin as Integer
ll_begin = 0

put this before the Do...Loop

Sekans
0
 

Author Comment

by:hess
ID: 1464716
Why are there to loop statements at the end of the answer is one not suposed to be there
0
 

Author Comment

by:hess
ID: 1464717
I'll test it now and if it works i'll give you the 135 points
0
 

Author Comment

by:hess
ID: 1464718
could you please rewrite the answer so that the whole szReplace function is included, From "Public Function" to "End Functioin" I've been working with vb for only a couple of days and i would like to be able to just paste it in.
0
 

Author Comment

by:hess
ID: 1464719
Never Mind. It works great and thanks for the help sekans and Brunchey. But why did you say to set II_Begin to 0 instead of 1 sekans
0
 
LVL 1

Expert Comment

by:Sekans
ID: 1464720
That was a mistake, 1 is correct.
0
 
LVL 1

Expert Comment

by:brunchey
ID: 1464721
Function szReplace(szText As String, szFind As String, szRepl As String) As String
'-- This function replaces all occurances of szFind with szRepl within
          '   szText and returns the resulting string.

              Dim nThere As Long
              Dim szTemp As String
              Dim ll_begin
              '-- Copy the incoming text so that szText is not altered.
              szTemp = szText
              ll_begin = 1
              '-- Go into a loop
              Do
                  '-- Is szFind part of szTemp?
                  nThere = InStr(ll_begin, szTemp, szFind)
                  If nThere Then
                      '-- Yes. nThere is now the position of szFind within szTemp
                     
                      '        [---------- A ----------]   [-B -]   [------------- C ----------------]
                      szTemp = Left$(szTemp, nThere - 1) & szRepl & Mid$(szTemp, nThere + Len(szFind))
                      ll_begin = nThere + Len(szRepl)
                  Else
                      '-- No. There is nothing more to do, so exit the loop
                      Exit Do
                  End If
              Loop

              '-- Assign szReplace to the szTemp string, which contains the
              '   modified original text.
              szReplace = szTemp
End Function

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

770 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