Results 1 to 15 of 15

Thread: Element Table Won't Work

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Post

    I am making a program to help my friends and I study for science, but this won't work. It won't clear the text box, so if you get one right, all you have to do is press OK again and it says "Correct". AND THE TEXT BOX WON'T CLEAR, I'VE TRIED EVERYTHING!


    Randomize
    MyValue = Int((14 * Rnd) + 1)
    If MyValue = "1" Then Label2.Caption = "Scandium"
    If Text1.Text = "Sc" Then Text1.Text = ""
    If Text1.Text = "Sc" Then MsgBox "Correct"

    If MyValue = "2" Then Label2.Caption = "Titanium"
    If Text1.Text = "Ti" Then Text1.Text = ""
    If Text1.Text = "Ti" Then MsgBox "Correct"

    If MyValue = "3" Then Label2.Caption = "Vanadium"
    If Text1.Text = "V" Then Text1.Text = ""
    If Text1.Text = "V" Then MsgBox "Correct"

    If MyValue = "4" Then Label2.Caption = "Chromium"
    If Text1.Text = "Cr" Then Text1.Text = ""
    If Text1.Text = "Cr" Then MsgBox "Correct"

    If MyValue = "5" Then Label2.Caption = "Manganese"
    If Text1.Text = "Mn" Then Text1.Text = ""
    If Text1.Text = "Mn" Then MsgBox "Correct"

    If MyValue = "6" Then Label2.Caption = "Iron"
    If Text1.Text = "Fe" Then Text1.Text = ""
    If Text1.Text = "Fe" Then MsgBox "Correct"

    If MyValue = "7" Then Label2.Caption = "Cobalt"
    If Text1.Text = "Co" Then Text1.Text = ""
    If Text1.Text = "Co" Then MsgBox "Correct"

    If MyValue = "8" Then Label2.Caption = "Nickel"
    If Text1.Text = "Ni" Then Text1.Text = ""
    If Text1.Text = "Ni" Then MsgBox "Correct"

    If MyValue = "9" Then Label2.Caption = "Copper"
    If Text1.Text = "Cu" Then Text1.Text = ""
    If Text1.Text = "Cu" Then MsgBox "Correct"

    If MyValue = "10" Then Label2.Caption = "Zinc"
    If Text1.Text = "Zn" Then Text1.Text = ""
    If Text1.Text = "Zn" Then MsgBox "Correct"

    If MyValue = "11" Then Label2.Caption = "Gallium"
    If Text1.Text = "Ga" Then Text1.Text = ""
    If Text1.Text = "Ga" Then MsgBox "Correct"

    If MyValue = "12" Then Label2.Caption = "Germanium"
    If Text1.Text = "Ge" Then Text1.Text = ""
    If Text1.Text = "Ge" Then MsgBox "Correct"
    If MyValue = "13" Then Label2.Caption = "Arsenic"
    If Text1.Text = "As" Then Text1.Text = ""
    If Text1.Text = "As" Then MsgBox "Correct"

    If MyValue = "14" Then Label2.Caption = "Selenium"
    If Text1.Text = "Se" Then Text1.Text = ""
    If Text1.Text = "Se" Then MsgBox "Correct"

    I'm Sorry The Code Is So Long, But We Need To Study 14 Elements.

    Please Help. I don't like text books, this way I can study while at my computer

    Steve

  2. #2
    Addicted Member
    Join Date
    Jan 1999
    Posts
    239

    Post

    Private Sub Text1_LostFocus()
    Text1 = ""
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308

    Post

    You got wrong sequence of code:

    If MyValue = "1" Then Label2.Caption = "Scandium"

    If Text1.Text = "Sc" Then MsgBox "Correct"
    If Text1.Text = "Sc" Then Text1.Text = ""

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Post

    LG, I tried that, but it still won't clear the text box.

    Steve

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    I should think VB is getting confused because both lines have the same condition but differing outcomes...look...

    If Text1.Text = "Sc" Then Text1.Text = ""
    If Text1.Text = "Sc" Then MsgBox "Correct"

    You are effectively saying "If Text1 is Sc, then make it blank BUT If Text1 is Sc (same IF condition!!) then show Correct Message".

    You want to put:

    If Text1.Text = "Sc" Then
    MsgBox "Correct"
    Else
    Text1.Text=""
    End if

    Hope this helps

    Regards,


    ------------------
    - Chris
    chris.kilhams@btinternet.com
    If it ain't broke - don't fix it

  6. #6
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305

    Post

    Or, to make the program a little fancier,

    Code:
    If MyValue = "#" Then Label2.Caption = "XXXXXX"
    If Text1.Text = "XX" Then
    correct = MsgBox ("Correct", vbOKOnly, "Correct")
    Text1.Text = ""
    Else
    wrong = MsgBox ("Incorrect", vbOKOnly, "Incorrect")
    Text1.Text = ""
    End If
    also, you might consider using Select Case for each of the MyValue parts rather than If Then.

    bob

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Post

    Bob, the code still won't clear the text box. And if you get it right, you only have to keep pressing ok and it says correct. I can send you the Exe if you want to see what I mean.

    Steve

  8. #8
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    Give my suggestion a whirl - often making things "fancier" just results in them exploding and making lots of mess (bodies, guts, blood etc)

    Amused Regards,

    ------------------
    - Chris
    chris.kilhams@btinternet.com
    If it ain't broke - don't fix it

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    Steve - Here I was deciding if i should go to MacDonalds for lunch, or spend my lunch time helping a fellow VB-er. Anyway, I went for the latter (my stomach would probably have gone for the former...!). I spent the last 20 mins doing the project for you. Here is all the code, simply paste into the Declarations section of a form, place a Label called Label2, a Textbox called Text1 and a command button called Command1.

    It is guaranteed to work (it works fine on mine anyway). At least you've probably already learnt the symbols, I can do most of them first time and I was only messing with code for 15 mins (haven't done elements for years ). Best of luck.

    Dim MyValue As Integer

    Sub NewPoser()

    Randomize

    MyValue = Int((14 * Rnd) + 1)

    If MyValue = "1" Then
    Label2.Caption = "Scandium"
    Text1.Text = ""
    End If

    If MyValue = "2" Then
    Label2.Caption = "Titanium"
    Text1.Text = ""
    End If

    If MyValue = "3" Then
    Label2.Caption = "Vanadium"
    Text1.Text = ""
    End If

    If MyValue = "4" Then
    Label2.Caption = "Chromium"
    Text1.Text = ""
    End If

    If MyValue = "5" Then
    Label2.Caption = "Manganese"
    Text1.Text = ""
    End If

    If MyValue = "6" Then
    Label2.Caption = "Iron"
    Text1.Text = ""
    End If

    If MyValue = "7" Then
    Label2.Caption = "Cobalt"
    Text1.Text = ""
    End If

    If MyValue = "8" Then
    Label2.Caption = "Nickel"
    Text1.Text = ""
    End If

    If MyValue = "9" Then
    Label2.Caption = "Copper"
    Text1.Text = ""
    End If

    If MyValue = "10" Then
    Label2.Caption = "Zinc"
    Text1.Text = ""
    End If

    If MyValue = "11" Then
    Label2.Caption = "Gallium"
    Text1.Text = ""
    End If

    If MyValue = "12" Then
    Label2.Caption = "Germanium"
    Text1.Text = ""
    End If

    If MyValue = "13" Then
    Label2.Caption = "Arsenic"
    Text1.Text = ""
    End If

    If MyValue = "14" Then
    Label2.Caption = "Selenium"
    Text1.Text = ""
    End If

    End Sub

    Private Sub Command1_Click()

    If MyValue = "1" Then
    If Text1.Text = "Sc" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "2" Then
    If Text1.Text = "Ti" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "3" Then
    If Text1.Text = "V" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "4" Then
    If Text1.Text = "Cr" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "5" Then
    If Text1.Text = "Mn" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "6" Then
    If Text1.Text = "Fe" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "7" Then
    If Text1.Text = "Co" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "8" Then
    If Text1.Text = "Ni" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "9" Then
    If Text1.Text = "Cu" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "10" Then
    If Text1.Text = "Zn" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "11" Then
    If Text1.Text = "Ga" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "12" Then
    If Text1.Text = "Ge" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "13" Then
    If Text1.Text = "As" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If

    ElseIf MyValue = "14" Then
    If Text1.Text = "Se" Then
    MsgBox "Correct"
    NewPoser
    Text1.SetFocus
    Else
    Text1.Text = ""
    Text1.SetFocus
    End If
    End If

    End Sub

    Private Sub Form_Load()

    NewPoser

    End Sub

    Regards,

    ------------------
    - Chris
    chris.kilhams@btinternet.com
    If it ain't broke - don't fix it

  10. #10
    Lively Member
    Join Date
    Jul 1999
    Posts
    99

    Post

    if the suggestions here still don't work, try stepping one line at a time through your code by pressing F8.


  11. #11
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308

    Post

    Hi,Steve.

    This should work:
    If Text1.Text = "Se" Then
    MsgBox "Correct"
    Text1 = ""
    End If

    Larisa

  12. #12
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Post

    How about a bit more structured approach?
    Code:
    Option Explicit
    Private Type Element
        Name As String
        Code As String
    End Type
    Private Elements(1 To 14) As Element
    Private MyValue As Integer
    
    Sub NewPoser()
        Randomize Timer
        MyValue = Int((14 * Rnd) + 1)
        Label2.Caption = Elements(MyValue).Name
        Text1.Text = ""
    End Sub
    
    Private Sub Command1_Click()
        If Text1.Text = Elements(MyValue).Code Then
            MsgBox "Correct"
            NewPoser
        Else
            Text1.Text = ""
        End If
        Text1.SetFocus
    End Sub
    
    Private Sub Form_Load()
        Call LoadElements
        Call NewPoser
    End Sub
    
    Private Sub LoadElements()
        Elements(1).Code = "Sc"
        Elements(1).Name = "Scandium"
        Elements(2).Code = "Ti"
        Elements(2).Name = "Titanium"
        Elements(3).Code = "V"
        Elements(3).Name = "Vanadium"
        Elements(4).Code = "Cr"
        Elements(4).Name = "Chromium"
        Elements(5).Code = "Mn"
        Elements(5).Name = "Manganese"
        Elements(6).Code = "Fe"
        Elements(6).Name = "Iron"
        Elements(7).Code = "Co"
        Elements(7).Name = "Cobalt"
        Elements(8).Code = "Ni"
        Elements(8).Name = "Nickel"
        Elements(9).Code = "Cu"
        Elements(9).Name = "Copper"
        Elements(10).Code = "Zn"
        Elements(10).Name = "Zinc"
        Elements(11).Code = "Ga"
        Elements(11).Name = "Gallium"
        Elements(12).Code = "Ge"
        Elements(12).Name = "Germanium"
        Elements(13).Code = "As"
        Elements(13).Name = "Arsenic"
        Elements(14).Code = "Se"
        Elements(14).Name = "Selenium"
    End Sub

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Post

    Thank you all for helping me on this problem. I hate looking in books to study. I thought this was an interresting approach. Thanks to Chris, it did work, thanks for taking the time this. I'm using Chris's code, but thank you Frans, yours worked also. Now to study. If I get 100% on this, my grade for the morking period will be 90.09%!!!!!!!!!!! Whish me luck!

    Steve

  14. #14
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Post

    Good luck

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Post

    Thanks Guys I'm Done! If you want to see it email me. SteveCRM@aol.com Sorry it isn't too graphical, the test is tuesday so I didn't have much time


    Steve

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