Results 1 to 14 of 14

Thread: Need Help with my program

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    3

    Need Help with my program

    Hello everybody, first of all I have only taken one semester of Visual Basic at my high school so I am a beginner, and I am using Visual Basic 6.0.

    I am trying to build a program that will do this: Generate a key which is set up like this:

    XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

    But, the hard part is here. Let X represent letters (without vowels) and 1 represent numbers 1-9.

    When the command button is clicked I need a random key to be presented like this:

    XX1XX-1XXXX-XX1XX-XXXX1-11XXX

    There is about 20 different combinations of "XX1XX" and they will need to be randomly put into the code with random letters in the X spaces and random numbers in the 1 spaces.

    It's pretty complicated to explain, so post any questions you have.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need Help with my program

    Well my question is, is this a school project?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Need Help with my program

    I'm not going to do it all as it's for school but here is 90% of it. This function generates a key like xxxxx-xxxxx-xxxxx-xxxxx-xxxxx. It does not allow vowels, but as for putting numbers in where you said, thats the other 10%. It should be fairly easy to do that.

    This is the function, just call it, and it will return a key. No need to pass any arguments to it. This can be in a form or module.
    VB Code:
    1. Public Function GenerateKey() As String
    2.  
    3.     ' i - this is the loop counter for looping through each letter
    4.     ' of one part of the key e.g. 'djwhf'
    5.     Dim i As Integer
    6.     ' ii - this is the loop counter for looping through each part of the key
    7.     ' it loops 5 times (0 based), loop 5 times to generate 5 sections
    8.     ' e.g. sec1-sec2-sec3-sec4-sec5
    9.     Dim ii As Integer
    10.     ' strParts - this is a 0 based array, so it has 5 elements. Each element is
    11.     ' one of the 5 sections of the key
    12.     Dim strParts(4) As String
    13.     ' strChar - this string (length: 1) will contain the random numbers Chr() value. E.g. 97 value is 'a'
    14.     Dim strChar As String * 1
    15.     ' intRandom - this will store the random number that was generated between 97-122 (a-z)
    16.     Dim intRandom As Integer
    17.     ' blnVowel - this boolean (true/false) will be used when checking if the generated
    18.     ' letter is a vowel or not.
    19.     Dim blnVowel As Boolean
    20.     ' strResult - this string will contain the final key before it is returned
    21.     Dim strResult As String
    22.    
    23.     'ASCII Codes letters
    24.     'a = 97
    25.     'e = 101
    26.     'i = 105
    27.     'o = 111
    28.     'u = 117
    29.     'z = 122
    30.        
    31.     For ii = 0 To 4
    32.     'set ii = 0 and loop until it reaches 4, loop 5 times to generate
    33.     ' 5 sections e.g. sec1-sec2-sec3-sec4-sec5. ii will increment (+1)
    34.     ' each time it loops
    35.    
    36.         For i = 1 To 5
    37.         ' This loop generates 1 random letter that is not a vowel,
    38.         ' each time it runs. Loop 5 times to generate 5 letters
    39.             blnVowel = True
    40.             ' Set the vowel boolean to true so it doesn't skip the loop below
    41.             While blnVowel = True
    42.             ' This loop runs until we generate a character that is not a vowel
    43.                 intRandom = Int((122 - 97 + 1) * Rnd + 97)
    44.                 'generate the random number between 97-122 (a-z)
    45.                 If (intRandom = 97) Or (intRandom = 101) Or (intRandom = 105) Or (intRandom = 111) Or (intRandom = 117) Then
    46.                 ' This If checks if the generated number matched the ASCII value of any vowel
    47.                 ' If it does find a match, it needs to generate a new number and check again
    48.                 ' It does this over and over until it finds a number that does not match
    49.                 ' the ASCII of any vowel
    50.                     blnVowel = True
    51.                     ' It matches a vowel so set the boolean to TRUE
    52.                 Else
    53.                     ' its not a vowel so use it
    54.                     strChar = Chr(intRandom)
    55.                     ' Convert the number to its corresponding letter ans store in strChar
    56.                     strParts(ii) = strParts(ii) & strChar ' add the new letter onto the section
    57.                     blnVowel = False 'it was not a vowel so we dont want to run that loop again
    58.                 End If
    59.             Wend ' keep looping until the While condition is met
    60.         Next i ' loop with the next i
    61.        
    62.         If ii = 4 Then
    63.             ' This if statement simply checks if the section it generated is the final section or not
    64.             ' If it is the final section, add the section the the strResult but dont add
    65.             ' a - on the end. Without this If, the key would turn out xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-
    66.             strResult = strResult & strParts(ii)
    67.         Else
    68.             ' Its not the final section so add a - on the end of the result
    69.             ' To seperate it from the next section coming up
    70.             strResult = strResult & strParts(ii) & "-"
    71.         End If
    72.     Next ii ' loop with the next ii
    73.    
    74.     GenerateKey = strResult ' finished generating, return the key to what called it
    75.    
    76. End Function

    IMPORTANT: Put the 'Randomize' command in your form_load event. This ensures the number is random, if you forget this, the same key will be generated each time your app starts.
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Randomize
    4.  
    5. End Sub

    As I said to use the function, just call it, for example in a button:
    VB Code:
    1. Private Sub Command2_Click()
    2.    
    3.     Dim strKey As String
    4.    
    5.     strKey = GenerateKey()
    6.    
    7.     MsgBox strKey
    8.    
    9. End Sub
    welcome to VBF mate.
    Last edited by the182guy; Feb 7th, 2007 at 01:57 PM.
    Chris

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need Help with my program

    I took a slightly differnt approach.. in they way that u can set the pattern

    but, this is 100% done, and im not sure to post it or not. I dont like handing it on a silver platter.. lol

    I'll describe whats done...
    set a const = the pattern you have
    loop thru each char using mid
    if its an X - use a rnd to gen a letter testing for AEIOU
    if its a 1 - gen a rnd num 1-9
    if its a - then just copy to new key

    drop into a textbox
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Need Help with my program

    Just added comments to every line above
    Chris

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Need Help with my program

    Quote Originally Posted by Static
    I took a slightly differnt approach.. in they way that u can set the pattern

    but, this is 100% done, and im not sure to post it or not. I dont like handing it on a silver platter.. lol

    I'll describe whats done...
    set a const = the pattern you have
    loop thru each char using mid
    if its an X - use a rnd to gen a letter testing for AEIOU
    if its a 1 - gen a rnd num 1-9
    if its a - then just copy to new key

    drop into a textbox
    Don't tease him by saying 'I might or might not give you it'.
    Chris

  7. #7
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need Help with my program

    bah.. ok fine

    VB Code:
    1. Private Sub Command1_Click()
    2.     'Change the key to whatever pattern
    3.     Const sKEY As String = "XX1XX-1XXXX-XX1XX-XXXX1-11XXX"
    4.    
    5.     Dim x As Integer
    6.     Dim NewKey As String
    7.     'loop thru each piece of the key
    8.     For x = 1 To Len(sKEY)
    9.         If Mid(sKEY, x, 1) = "X" Then 'if x - use a letter
    10.             Do
    11.                 tmp = Chr(Int(Rnd(1) * 26) + 65) 'A-Z
    12.             Loop Until InStr("AEIOU", tmp) = 0 '<> AEIOU
    13.             NewKey = NewKey & tmp 'Add to new string
    14.         ElseIf Mid(sKEY, x, 1) = "1" Then
    15.             tmp = Int(Rnd(1) * 9) + 1 'rnd # 1-9
    16.             NewKey = NewKey & tmp 'Add to new string
    17.         Else
    18.             NewKey = NewKey & "-" 'Add to new string
    19.         End If
    20.     Next
    21.     Text1 = NewKey 'Drop new key into a textbox
    22. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Need Help with my program

    i'd probably do something like this:
    VB Code:
    1. Private Sub Command2_Click()
    2.     Debug.Print CreateKey("XX1XX-1XXXX-XX1XX-XXXX1-11XXX")
    3. End Sub
    4.  
    5. Private Function CreateKey(ByVal sPattern As String) As String
    6.     Const VALID As String = "BCDFGHJKLMNPQRSTVWXYZ"
    7.     Dim N As Long
    8.    
    9.     For N = 1 To Len(sPattern)
    10.         Select Case Mid$(sPattern, N, 1)
    11.             Case "1"
    12.                 Mid(sPattern, N, 1) = CStr(Int(Rnd * 9) + 1)
    13.             Case "X"
    14.                 Mid(sPattern, N, 1) = Mid$(VALID, Int(Len(VALID) * Rnd) + 1, 1)
    15.         End Select
    16.     Next N
    17.     CreateKey = sPattern
    18. End Function
    one loop, no concatenation

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need Help with my program

    ahh.. topped by bushmobile! very nice...

    squeezed into less lines... LOL

    VB Code:
    1. Private Function CreateKey(ByVal sPattern As String) As String
    2.     Const VALID As String = "BCDFGHJKLMNPQRSTVWXYZ"
    3.         Dim N As Long
    4.         For N = 1 To Len(sPattern)
    5.             Mid(sPattern, N, 1) = IIf(Mid(sPattern, N, 1) = "1", CStr(Int(Rnd * 9) + 1), IIf(Mid(sPattern, N, 1) = "X", Mid$(VALID, Int(Len(VALID) * Rnd) + 1, 1), "-"))
    6.         Next N
    7.     CreateKey = sPattern
    8. End Function
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Need Help with my program

    LoL, less lines doesnt = more efficient.

    It calls about 10 functions and checks 2 conditions on one line which is looped about 25 times. There wont be much difference in efficiency, and even if there is, that only made any difference pre windows 98, especially in an app with this nature.
    Chris

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need Help with my program

    I know.. just did it for fun
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    3

    Re: Need Help with my program

    Its not for school, I was just giving my background this program is for fun.

    But thanks for giving me advice and help anyways. I talked to my old VB teacher today and got some help from her too.

    I'll try these after work, thanks!

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Need Help with my program

    here's a significantly more efficient version:
    VB Code:
    1. Private Function CreateKey(ByRef sPattern As String) As String
    2.     Const VALID As String = "BCDFGHJKLMNPQRSTVWXYZ"
    3.     Dim b() As Byte, bytVALID() As Byte, N As Long
    4.     b = sPattern
    5.     bytVALID = VALID
    6.    
    7.     For N = 0 To UBound(b) Step 2
    8.         Select Case b(N)
    9.             Case 49 ' "1"
    10.                 b(N) = 49 + Int(Rnd * 9)
    11.             Case 88 ' "X"
    12.                 b(N) = bytVALID(Int(Rnd * (UBound(bytVALID) \ 2 + 1)) * 2)
    13.         End Select
    14.     Next N
    15.     CreateKey = b
    16. End Function

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    3

    Re: Need Help with my program

    hey 182guy your code really helped, but, I don't know how to make a set of the random combinations (xx1xx 1xxxx and so on, there is about 20) and make it so they will generate and put the letters and numbers in the correct spot.

    Your codes really advanced for me lol, can you help me out haha, you don't have to but it would be so greatly appreciated.

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