Apr 9th, 2005, 08:57 AM
#1
Thread Starter
Lively Member
Replace Hyphon with Number(RESoLvED)
You have probably already seen this thread, I am posting it again because I have yet to find an answer. I just need a simple way to take a line of hyphons (-----------) and replace any hyphon with the number of my choosing. Also, I need to be able to do this for multiple lines.
Any Suggestions that will help a simple person such as I?
Last edited by nickTHEguitarist; Apr 9th, 2005 at 09:34 AM .
Nick
Apr 9th, 2005, 09:03 AM
#2
Re: Replace Hyphon with Number
Something like...
---3----1----2--
----1---2----3--
Well - you need to show a bit of code with what you have these strings of hyphens in right now.
And a bit of logic on how you are going to decide what number goes into what spot.
The simple answer to your question is:
Code:
Dim strText as String, strNew as String, lngPos as Long
strText = "---------"
Debug.Print strText
lngPos = 5
strNew = "1"
strText = Left$(strText,lngPos-1) & strNew & Mid$(strText,lngPos+1)
Debug.Print strText
The real question is how do you decide on the value of lngPos and the value of strNew?
[edit] - oops - needed to use MID$ - not RIGHT$
Last edited by szlamany; Apr 9th, 2005 at 11:33 AM .
Apr 9th, 2005, 09:17 AM
#3
Thread Starter
Lively Member
Re: Replace Hyphon with Number
It will be easy to decide what strNew would be. What would be the output of that code?
Apr 9th, 2005, 09:23 AM
#4
Re: Replace Hyphon with Number
replace(text1.text,"-",'1)
then just loop thru it
Apr 9th, 2005, 09:27 AM
#5
Thread Starter
Lively Member
Re: Replace Hyphon with Number
Ah, that is an answer I like one line of code
Thanks
Apr 9th, 2005, 09:29 AM
#6
Re: Replace Hyphon with Number
But that replaces all the - hyphens with then number 1.
Not quite what you are looking for - but you are not really explaining what you are looking for.
Put my code into a FORM_LOAD event in a new project and see what it does.
Apr 9th, 2005, 09:34 AM
#7
Thread Starter
Lively Member
Re: Replace Hyphon with Number
Yes you're right, your code does work alot better. Thanks.
Apr 9th, 2005, 09:39 AM
#8
Re: Replace Hyphon with Number(RESoLvED)
gg szlamany..
just as some info you could call a different sub and change the value of x then it will come back and you could change it that way...heres some code for it
VB Code:
private sub cmd_click()
'Loop and stuff here
dim x as integer
replace(text1.text,"-",x)
call rewrite
end sub
private sub rewrite()
x = x+1
end sub
Apr 9th, 2005, 09:44 AM
#9
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
Yes, but szlamany's way seems easier and more understandable to me. Thanks for you input tho remix. It's appreciated
Apr 9th, 2005, 11:36 AM
#10
Re: Replace Hyphon with Number(RESoLvED)
Nick - here's answer an to your PM.
Code:
strB = "----------------------------------------"
txtAmajSpc.Text = intAmajSpc
lngPos = Val(intAmajSpc)
' lngPos = lngPos
strFretNumb = "2"
strB = Left$(strB, lngPos - 1) & strFretNumb & Mid$(strB, lngPos + 1)
Need to use MID$, not RIGHT$ - my earlier post was wrong - I typed it free hand...
The way I show it above it's only going to remove a single hyphen and replace it with the strFretNumb string.
As to your other question - INTEGER is a reserved word - you cannot use it as a variable name - if that's what you meant.
Put OPTION EXPLICIT at the top of your FORM, so that VARIABLE name DIM's are required - will make your programming life easier in general.
Please respond back to this thread with questions - father keep it on board as opposed to PM.
Apr 9th, 2005, 11:39 AM
#11
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
Yes I know, I used integer just as a quick example. And that is exactly what I want. One space replaced by strFretNumb. Thanks for your help!
Slow ride, take it easy.
Apr 9th, 2005, 11:45 AM
#12
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
Hmm. It still says runtime error 5...
VB Code:
txtAmajSpc.Text = intAmajSpc
lngPos = Val(intAmajSpc)
lngPos = lngPos
strFretNumb = "2"
[COLOR=Red] strB = Left$(strB, lngPos - 1) & strFretNumb & Mid$(strB, lngPos + 36)[/COLOR]
It still says the code in read is invalid.
Apr 9th, 2005, 11:56 AM
#13
Re: Replace Hyphon with Number(RESoLvED)
Originally Posted by
nickTHEguitarist
Hmm. It still says runtime error 5...
VB Code:
txtAmajSpc.Text = intAmajSpc
lngPos = Val(intAmajSpc)
lngPos = lngPos
strFretNumb = "2"
[COLOR=Red] strB = Left$(strB, lngPos - 1) & strFretNumb & Mid$(strB, lngPos + 36)[/COLOR]
It still says the code in read is invalid.
Use lngPos + 1, not lngPos + 36
What is the value of lngPos anyway?
Apr 9th, 2005, 11:58 AM
#14
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
how many spaces over in my line of hyphons the number is placed
Apr 9th, 2005, 12:00 PM
#15
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
No, that doesn't work. I think the number is getting lost between the text box to the variable, to the long. Do you mind looking over my project real quick to see whats the matter? I'll send it once you reply back.
Apr 9th, 2005, 12:04 PM
#16
Re: Replace Hyphon with Number(RESoLvED)
Go ahead - either post the code in [Highlight=VB] tags right here - or ZIP it up.
Apr 9th, 2005, 12:07 PM
#17
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
I'll zip it up. Try not to change too much because you might put stuff in there that I'll have no idea what it is.
Attached Files
Apr 9th, 2005, 01:22 PM
#18
Re: Replace Hyphon with Number(RESoLvED)
I downloaded it but I'm not really sure how to run your app...
I still see lots of RIGHT$ statements in the code.
That was a typo on my part - in my first post in this thread.
All those RIGHT$(xxx,lngPos+36) should be MID$(xxx,lngPos+1).
A suggestion for debugging - from the TOOLS menu - OPTIONS - General Tab - select the BREAK ON ALL ERRORS radio button. This will cause the code to stop on an error so you can hover over variables and see what values they have.
Good luck - I'm out of here for a couple of hours.
Apr 9th, 2005, 01:41 PM
#19
Thread Starter
Lively Member
Re: Replace Hyphon with Number(RESoLvED)
Idk... this won't let me use variables instead of lngPos = 2. I need to put a variable in there instead of 2. AHH
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width