borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Print reports in an ACCESS database)Next Topic (databound combobox) New Topic New Poll Post Reply
AndreaVB Forum : Database : Check Boxes
Poster Message
Shady
Level: VB Guru


Registered: 08-07-2002
Posts: 305

icon Check Boxes  Archived to Disk

I am attempting to program a check box and seem to be having a little trouble.  What I want is for the program to display another form when a check box is clicked.  The way in which I have been trying to do this is as follows

Sub Check_Click

if CheckBox = true then
     NewForm.show
     OldForm.enabled=false
end if

what happens is that on loading if the check box is true the code is executed, whereas I required the code only to be executed when the check box is clicked and when it is set to true.

Any Ideas

____________________________
I don't wanna die... but I ain't keen on livin' either

26-07-2002 at 04:21 PM
View Profile Send Email to User Show All Posts | Quote Reply
Coyote
Level: Guest

icon Re: Check Boxes  Archived to Disk

Here is an example: Make a new project and add a form(form2) - on Form1 add a check box
then copy and paste this code into Form1:
' CODE BEGINS FOR FORM ONE
Private Sub Check1_Click()
    If Check1.Value = Checked Then
        Form2.Show
        Form1.Hide
        Unload Form1
    End If
    
End Sub

Sub Check_Click()
If Check1.Value = Checked Then
        Form2.Show
        Form1.Hide
        Unload Form1
    End If
    
End Sub

Private Sub Form_Load()
    ' This will check to see if value is
    ' set to checked when the form opens
    Check_Click
End Sub

' CODE FOR FORM2 Copy and paste
Private Sub Form_Load()
    MsgBox "Hi I'm form2 and open", vbOKOnly
End Sub

' END OF CODE  *******

This will check to see if the check box is pre-set to checked - if so form2 will display
If NOT pre-set to checked.. then when checked it will open form2....

Hope this helps...
>>>Coyote<<<

27-07-2002 at 02:12 AM
| Quote Reply
Shady
Level: VB Guru


Registered: 08-07-2002
Posts: 305
icon Re: Check Boxes  Archived to Disk

OK so I don't think I have explained myself very well, I'll try again.

The main Form (DatabaseEntry.frm) has on it a check box (DDCheck) and in the DDCheck_Click I have the following code:-



Private Sub DDCheck_Click()

'Check for Direct Debit Check and display reminder
If DDCheck.Value = Checked Then
      DetailsReview.Show
      DatabaseEntry.Hide
      Unload DatabaseEntry
End If

End Sub

And so to the problem in hand.  If DatabaseEntry loads and the DDCheck is set to checked, DatabasEntry promptly unloads causing an "Object was unloaded" error.  The problem is that the CheckBox is linked to a database, therfore if the first record of the database has DDCheck set as Checked, the code is executed immediately and the program crahes and burns.  I need the code only to be executed if the CheckBox is physically changed, not when the form first loads.

Have a go at that if ya dare!!!



____________________________
I don't wanna die... but I ain't keen on livin' either

29-07-2002 at 04:23 PM
View Profile Send Email to User Show All Posts | Quote Reply
Coyote
Level: Guest

icon Re: Check Boxes  Archived to Disk

Hello Shady,
I am guessing that you have a DataControl linked to a database on form1 and the check box is bound to the datacontrol. But not sure that matters, since your just testing for one control - DDCheck.

So if you take the above code I gave you and just remove(delete) all the Sub Check_Click( ) code and all the code for the Private Sub Form_Load() in the Form1 code, then everything will work as you asked.

HOWEVER - if your using the value or fact that you clicked the check box on Form1 in your code on Form2 for some purpose. You'll get the error you described. So I am gussing from this--- that is the case.

For example: If I click the check box in Form1, I want to make Form2 Green.  I'm Unloading form1 but I need to know in form2 if the check box in form1 was clicked.

There are about three ways to do this. One will not apply since you are unloading form1. To do this you need to set a variable which is available to all forms. The example below should be the answer:
Start with a new project- add a command button and then put a check box on form1 and rename it DDCheck. then copy and paste the following code behind form1:
' FORM1 CODE STARTS *****
Private Sub Command1_Click()
    CheckIt = False
    Form2.Show
    Form1.Hide
    Unload Form1
End Sub

Private Sub DDCheck_Click()
    If DDCheck.Value = Checked Then
        CheckIt = True
        Form2.Show
        Form1.Hide
        Unload Form1
        
    End If
    
End Sub

' END OF FORM1 CODE ******

'Next Add a Module to your project and add this line of code, this is going to pass the fact that the check box was "CLICKED" and make it available to EVERY form in your project.

' MODULE1 CODE STARTS
Global CheckIt As Boolean
'MODULE1 CODE ENDS

' Next add a Form (Form2) and add a textbox (may want to set the Multiline property to True) then-place the following code behind Form2.
' FORM2 CODE STARTS *******
Private Sub Form_Load()
    
    If CheckIt = True Then
        Text1.Text = "You Clicked the check box on Form1"
        Form2.BackColor = vbGreen
        Else
        Text1.Text = "I'm Form2, but you DID NOT CLICK the check box on Form1"
        Form2.BackColor = vbRed
    End If
    
End Sub
' FORM2 CODE ENDS*******

'Run this and it should work and resolve your problem...

Additional information++++++++++
You can revise the above to actually pass whether the check box was checked or un-checked if you like.
For example:
Private Sub DDCheck_Click()
    If DDCheck.Value = Checked Then
        CheckIt = True ' This means you checked the check box
        Else
        CheckIt = False  ' This means you un-checked the check box
   End If
        Form2.Show
        Form1.Hide
        Unload Form1
End Sub
' You'll need to change the code under command1 also for this new code logic...

Anyway.. hope this helps .....
>>>Coyote<<<

30-07-2002 at 03:16 PM
| Quote Reply
AndreaVB Forum : Database : Check Boxes
Previous Topic (Print reports in an ACCESS database)Next Topic (databound combobox) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder