Results 1 to 25 of 25

Thread: [RESOLVED] Visual Basic newbie with problems

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    Resolved [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

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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.

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    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!

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    Re: Visual Basic newbie with problems

    that just gives me the time and date in a message box ?

  6. #6
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    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?

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Visual Basic newbie with problems

    Hmmm, my intoxicated mind came up with:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     MsgBox ret_num("bla (15)")
    5.     MsgBox ret_num("bla bla (this wont work)")
    6.     MsgBox ret_num("bla bla (275)")
    7. End Sub
    8.  
    9. Private Function ret_num(ByVal strIn As String) As Integer
    10.     Dim strArr() As String
    11.    
    12.     If InStr(strIn, "(") <> 0 Then
    13.         strArr = Split(strIn, "(")
    14.         If IsNumeric(Replace(strArr(1), ")", "")) Then
    15.             ret_num = CInt(Replace(strArr(1), ")", ""))
    16.         Else
    17.             ret_num = -1
    18.         End If
    19.     End If
    20.  
    21. End Function

    Which will yeild:
    15
    -1
    275

    -1 being a no go

  9. #9
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    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.

  10. #10
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Visual Basic newbie with problems

    Nice one Bruce.

    vb Code:
    1. Private Sub Command1_Click()
    2. Dim myStr As String
    3. Dim x As Long
    4. Dim newVal As String, posVal As String
    5.  
    6. myStr = "blah (15)"
    7.  
    8. For x = 1 To Len(myStr)
    9.    posVal = Mid$(myStr, x, 1)
    10.     If IsNumeric(posVal) Then
    11.         newVal = newVal & posVal
    12.     Else
    13.         Exit Sub ' In case there's no number
    14.     End If
    15. Next
    16.  
    17. MsgBox DateAdd("s", CInt(newVal), Now)
    18.  
    19. End Sub

    I added the exit sub.

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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

  12. #12
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Visual Basic newbie with problems

    Quote Originally Posted by zynder
    Nice one Bruce.
    TY

    There is of course merit in your approach.

  13. #13
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Visual Basic newbie with problems

    yea it should work fine with what Zynder added

  14. #14
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Visual Basic newbie with problems

    Quote 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.

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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

  16. #16
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Visual Basic newbie with problems

    Could you resolve this thread if your problem is resolved please Good Luck.

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    Re: Visual Basic newbie with problems

    Ok new problem - hate me yet ?

    I want to format 15 as minutes.. i.e. 0:15

  18. #18
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  19. #19
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Visual Basic newbie with problems

    24 hour time, instead of 12 hour time?

  20. #20
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Visual Basic newbie with problems

    Does your CheckBox's (mentioned in post #1) relate to minutes? ie 15, 30, 45 etc?

  21. #21

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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?

  22. #22
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Visual Basic newbie with problems

    Quote 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 .......

  23. #23

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    Re: Visual Basic newbie with problems

    Quote Originally Posted by Bruce Fox
    Well, now it's time (no pun intended) for you do some work for yourself .......

  24. #24
    Fanatic Member drivenbywhat's Avatar
    Join Date
    Jan 2007
    Location
    VA - USA
    Posts
    866

    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

  25. #25

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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
  •  



Click Here to Expand Forum to Full Width