|
-
May 11th, 2007, 05:10 AM
#1
Thread Starter
New Member
[RESOLVED] Visual Basic newbie with problems
Hey guys
Im new to Visual Basic and I'm having problems with an assignment. wondering if anyone can help me out.
My first problem has been driving me nuts!! Ill try explain as best as I can.
Ive got to somehow extract numbers from a string. The string looks something like this: "blah (15)" I have to get the 15 part out of the string and use it for something else. the string is part of a check box.
Second problem is.. I have to take that 15 and add it to a time. so if the time was 3:55 and I added the 15 it should make the time 4:10.
If anyone knows what I'm talking about and can help i will be eternally greatful1
-
May 11th, 2007, 05:18 AM
#2
Re: Visual Basic newbie with problems
Welcome to the Forum wastedben 
Please post more of the example string.
Last edited by Bruce Fox; May 11th, 2007 at 05:37 AM.
-
May 11th, 2007, 05:26 AM
#3
Thread Starter
New Member
Re: Visual Basic newbie with problems
Hmm i dont know what else to say..
The string is simply a word with a number in brackets. Its for an imaginary bake house. There are about 20 check boxes each with an item such as "Bread (15)" - the number is the cook time. So when its checked and a button is clicked it works out the cook time and then adds it to another a list. Ive been told that you can write a function or something that can be called on whenever I need to extract the cooktime, but i have no idea how.
-
May 11th, 2007, 05:26 AM
#4
Re: Visual Basic newbie with problems
Ok got a while to write this code Try this although there might be other approach on this.
Code:
Private Sub Command1_Click()
Dim myStr As String
Dim x As Long
Dim newVal As String, posVal As String
myStr = "blah (15)"
For x = 1 To Len(myStr)
posVal = Mid$(myStr, x, 1)
If IsNumeric(posVal) Then
newVal = newVal & posVal
End If
Next
MsgBox DateAdd("s", CInt(newVal), Now)
End Sub
Hope it helps buddy!
-
May 11th, 2007, 05:32 AM
#5
Thread Starter
New Member
Re: Visual Basic newbie with problems
that just gives me the time and date in a message box ?
-
May 11th, 2007, 05:33 AM
#6
Re: Visual Basic newbie with problems
Zynders code will do But just on the string, the reason i believe Bruce Fox asked is because it might be possibly changing not static in other words. So will it continuously change?
-
May 11th, 2007, 05:43 AM
#7
Thread Starter
New Member
Re: Visual Basic newbie with problems
Ok so i hacked at that code abit..
Code:
Dim myStr As String
Dim x As Long
Dim newVal As String, posVal As String
myStr = "blah (15)"
For x = 1 To Len(myStr)
posVal = Mid$(myStr, 7, 2)
Next
MsgBox(posVal)
That managed to return the 15 in a text box.
Probably is that the number wont always be in the same position
-
May 11th, 2007, 05:47 AM
#8
Re: Visual Basic newbie with problems
Hmmm, my intoxicated mind came up with:
vb Code:
Option Explicit
Private Sub Form_Load()
MsgBox ret_num("bla (15)")
MsgBox ret_num("bla bla (this wont work)")
MsgBox ret_num("bla bla (275)")
End Sub
Private Function ret_num(ByVal strIn As String) As Integer
Dim strArr() As String
If InStr(strIn, "(") <> 0 Then
strArr = Split(strIn, "(")
If IsNumeric(Replace(strArr(1), ")", "")) Then
ret_num = CInt(Replace(strArr(1), ")", ""))
Else
ret_num = -1
End If
End If
End Function
Which will yeild:
15
-1
275
-1 being a no go
-
May 11th, 2007, 05:48 AM
#9
Re: Visual Basic newbie with problems
It doesnt matter where you put that number it searches character by character if found a number then it will separate it assuming your text is always like that otherwise more than thousand numbers it will return an overflow.
-
May 11th, 2007, 05:51 AM
#10
Re: Visual Basic newbie with problems
Nice one Bruce.
vb Code:
Private Sub Command1_Click()
Dim myStr As String
Dim x As Long
Dim newVal As String, posVal As String
myStr = "blah (15)"
For x = 1 To Len(myStr)
posVal = Mid$(myStr, x, 1)
If IsNumeric(posVal) Then
newVal = newVal & posVal
Else
Exit Sub ' In case there's no number
End If
Next
MsgBox DateAdd("s", CInt(newVal), Now)
End Sub
I added the exit sub.
-
May 11th, 2007, 05:57 AM
#11
Thread Starter
New Member
Re: Visual Basic newbie with problems
hmm nothing happens with that code for me .. My vb has a problem with newVal being used without being assigned a value
-
May 11th, 2007, 06:00 AM
#12
Re: Visual Basic newbie with problems
 Originally Posted by zynder
Nice one Bruce.
TY 
There is of course merit in your approach.
-
May 11th, 2007, 06:05 AM
#13
Re: Visual Basic newbie with problems
yea it should work fine with what Zynder added
-
May 11th, 2007, 06:05 AM
#14
Re: Visual Basic newbie with problems
 Originally Posted by wastedben
hmm nothing happens with that code for me .. My vb has a problem with newVal being used without being assigned a value
There is a value assigned.
-
May 11th, 2007, 06:15 AM
#15
Thread Starter
New Member
Re: Visual Basic newbie with problems
Ok I got Bruce's code to work .. but i put the sub AFTER the function.. then it got working.
Ok .. sorry if im slow but im not a natural coder lol. I need to work out my next question bbs
-
May 11th, 2007, 06:18 AM
#16
Re: Visual Basic newbie with problems
Could you resolve this thread if your problem is resolved please Good Luck.
-
May 11th, 2007, 06:26 AM
#17
Thread Starter
New Member
Re: Visual Basic newbie with problems
Ok new problem - hate me yet ? 
I want to format 15 as minutes.. i.e. 0:15
-
May 11th, 2007, 06:28 AM
#18
Re: Visual Basic newbie with problems
New topics should be in new threads.
Where does the 15 come from? A textbox?
Using the Format function should work for you, but we need to know where you are getting the value that needs to be formatted.
-
May 11th, 2007, 06:30 AM
#19
Re: Visual Basic newbie with problems
24 hour time, instead of 12 hour time?
-
May 11th, 2007, 06:33 AM
#20
Re: Visual Basic newbie with problems
Does your CheckBox's (mentioned in post #1) relate to minutes? ie 15, 30, 45 etc?
-
May 11th, 2007, 06:33 AM
#21
Thread Starter
New Member
Re: Visual Basic newbie with problems
ohh i figured it out
Format$((ret_num("blah(15)")), "0:00")
Teachers wants 12hr
Now how can I add 2 times together?
-
May 11th, 2007, 06:37 AM
#22
Re: Visual Basic newbie with problems
 Originally Posted by wastedben
Now how can I add 2 times together?
Well, now it's time (no pun intended) for you do some work for yourself .......
-
May 11th, 2007, 06:39 AM
#23
Thread Starter
New Member
Re: Visual Basic newbie with problems
 Originally Posted by Bruce Fox
Well, now it's time (no pun intended) for you do some work for yourself  .......
-
May 11th, 2007, 01:14 PM
#24
Re: Visual Basic newbie with problems
Not sure if this will work but here's a concept you can try:
ret_num = the time you extracted & formatted in 0:00
now get the current time and make sure it's in the same format
cooktime = ret_num + cur_time 'not sure if that works or not
the way I would have originally done it was, once you get the # extracted, in your example, 15, I would have extracted the last 2 digits from the current time.
cur_time = 4:21
last_digits1 = do a string search n get 21 from it
last_digits2 = last_digits1 + ret_num
if lastdigits2 >= 60 then assign it to another variable and subtract 60 from it
else
combine the current hour with your new last digits.
Then I again, I just might have confused you even more
-
May 12th, 2007, 01:00 AM
#25
Thread Starter
New Member
Re: Visual Basic newbie with problems
That does help! Thankyou.
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
|