|
-
Feb 7th, 2007, 12:02 PM
#1
Thread Starter
New Member
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.
-
Feb 7th, 2007, 01:16 PM
#2
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"
-
Feb 7th, 2007, 01:38 PM
#3
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:
Public Function GenerateKey() As String
' i - this is the loop counter for looping through each letter
' of one part of the key e.g. 'djwhf'
Dim i As Integer
' ii - this is the loop counter for looping through each part of the key
' it loops 5 times (0 based), loop 5 times to generate 5 sections
' e.g. sec1-sec2-sec3-sec4-sec5
Dim ii As Integer
' strParts - this is a 0 based array, so it has 5 elements. Each element is
' one of the 5 sections of the key
Dim strParts(4) As String
' strChar - this string (length: 1) will contain the random numbers Chr() value. E.g. 97 value is 'a'
Dim strChar As String * 1
' intRandom - this will store the random number that was generated between 97-122 (a-z)
Dim intRandom As Integer
' blnVowel - this boolean (true/false) will be used when checking if the generated
' letter is a vowel or not.
Dim blnVowel As Boolean
' strResult - this string will contain the final key before it is returned
Dim strResult As String
'ASCII Codes letters
'a = 97
'e = 101
'i = 105
'o = 111
'u = 117
'z = 122
For ii = 0 To 4
'set ii = 0 and loop until it reaches 4, loop 5 times to generate
' 5 sections e.g. sec1-sec2-sec3-sec4-sec5. ii will increment (+1)
' each time it loops
For i = 1 To 5
' This loop generates 1 random letter that is not a vowel,
' each time it runs. Loop 5 times to generate 5 letters
blnVowel = True
' Set the vowel boolean to true so it doesn't skip the loop below
While blnVowel = True
' This loop runs until we generate a character that is not a vowel
intRandom = Int((122 - 97 + 1) * Rnd + 97)
'generate the random number between 97-122 (a-z)
If (intRandom = 97) Or (intRandom = 101) Or (intRandom = 105) Or (intRandom = 111) Or (intRandom = 117) Then
' This If checks if the generated number matched the ASCII value of any vowel
' If it does find a match, it needs to generate a new number and check again
' It does this over and over until it finds a number that does not match
' the ASCII of any vowel
blnVowel = True
' It matches a vowel so set the boolean to TRUE
Else
' its not a vowel so use it
strChar = Chr(intRandom)
' Convert the number to its corresponding letter ans store in strChar
strParts(ii) = strParts(ii) & strChar ' add the new letter onto the section
blnVowel = False 'it was not a vowel so we dont want to run that loop again
End If
Wend ' keep looping until the While condition is met
Next i ' loop with the next i
If ii = 4 Then
' This if statement simply checks if the section it generated is the final section or not
' If it is the final section, add the section the the strResult but dont add
' a - on the end. Without this If, the key would turn out xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-
strResult = strResult & strParts(ii)
Else
' Its not the final section so add a - on the end of the result
' To seperate it from the next section coming up
strResult = strResult & strParts(ii) & "-"
End If
Next ii ' loop with the next ii
GenerateKey = strResult ' finished generating, return the key to what called it
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:
Private Sub Form_Load()
Randomize
End Sub
As I said to use the function, just call it, for example in a button:
VB Code:
Private Sub Command2_Click()
Dim strKey As String
strKey = GenerateKey()
MsgBox strKey
End Sub
welcome to VBF mate.
Last edited by the182guy; Feb 7th, 2007 at 01:57 PM.
Chris
-
Feb 7th, 2007, 01:55 PM
#4
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"
-
Feb 7th, 2007, 01:58 PM
#5
Re: Need Help with my program
Just added comments to every line above
-
Feb 7th, 2007, 02:03 PM
#6
Re: Need Help with my program
 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'.
-
Feb 7th, 2007, 02:13 PM
#7
Re: Need Help with my program
bah.. ok fine
VB Code:
Private Sub Command1_Click()
'Change the key to whatever pattern
Const sKEY As String = "XX1XX-1XXXX-XX1XX-XXXX1-11XXX"
Dim x As Integer
Dim NewKey As String
'loop thru each piece of the key
For x = 1 To Len(sKEY)
If Mid(sKEY, x, 1) = "X" Then 'if x - use a letter
Do
tmp = Chr(Int(Rnd(1) * 26) + 65) 'A-Z
Loop Until InStr("AEIOU", tmp) = 0 '<> AEIOU
NewKey = NewKey & tmp 'Add to new string
ElseIf Mid(sKEY, x, 1) = "1" Then
tmp = Int(Rnd(1) * 9) + 1 'rnd # 1-9
NewKey = NewKey & tmp 'Add to new string
Else
NewKey = NewKey & "-" 'Add to new string
End If
Next
Text1 = NewKey 'Drop new key into a textbox
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2007, 02:37 PM
#8
Re: Need Help with my program
i'd probably do something like this:
VB Code:
Private Sub Command2_Click()
Debug.Print CreateKey("XX1XX-1XXXX-XX1XX-XXXX1-11XXX")
End Sub
Private Function CreateKey(ByVal sPattern As String) As String
Const VALID As String = "BCDFGHJKLMNPQRSTVWXYZ"
Dim N As Long
For N = 1 To Len(sPattern)
Select Case Mid$(sPattern, N, 1)
Case "1"
Mid(sPattern, N, 1) = CStr(Int(Rnd * 9) + 1)
Case "X"
Mid(sPattern, N, 1) = Mid$(VALID, Int(Len(VALID) * Rnd) + 1, 1)
End Select
Next N
CreateKey = sPattern
End Function
one loop, no concatenation
-
Feb 7th, 2007, 02:49 PM
#9
Re: Need Help with my program
ahh.. topped by bushmobile! very nice...
squeezed into less lines... LOL
VB Code:
Private Function CreateKey(ByVal sPattern As String) As String
Const VALID As String = "BCDFGHJKLMNPQRSTVWXYZ"
Dim N As Long
For N = 1 To Len(sPattern)
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), "-"))
Next N
CreateKey = sPattern
End Function
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2007, 03:04 PM
#10
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.
-
Feb 7th, 2007, 03:08 PM
#11
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"
-
Feb 7th, 2007, 03:26 PM
#12
Thread Starter
New Member
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!
-
Feb 7th, 2007, 04:19 PM
#13
Re: Need Help with my program
here's a significantly more efficient version:
VB Code:
Private Function CreateKey(ByRef sPattern As String) As String
Const VALID As String = "BCDFGHJKLMNPQRSTVWXYZ"
Dim b() As Byte, bytVALID() As Byte, N As Long
b = sPattern
bytVALID = VALID
For N = 0 To UBound(b) Step 2
Select Case b(N)
Case 49 ' "1"
b(N) = 49 + Int(Rnd * 9)
Case 88 ' "X"
b(N) = bytVALID(Int(Rnd * (UBound(bytVALID) \ 2 + 1)) * 2)
End Select
Next N
CreateKey = b
End Function
-
Feb 7th, 2007, 10:04 PM
#14
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|