I've got an issue with my VBA code. Checking a check box and enabling text boxes.

FoxRocks

Registered User.
Local time
Today, 23:10
Joined
Apr 24, 2012
Messages
35
Hello,

Thank you for your interest in my problem. I'm currently having an issue with my VBA code (posted below)...or should I say, I don't know what I'm doing and I don't know how to fix my problem :)
So I've got a check box, that when selected "true", enables a text box and visa-versa. The function seems to work good, but when I've got two problems. One is that when the form opens, even though the check box is selected, it has the text box disabled (with data entered in the box).
The other problem is that I don't know how to have two instances of the same code. For example, I've got my "Parts_On_Order" set up in "Form_Current" and I would also like to have the exact same thing for "Parts_Required".
I think I'm actually talking about the same problem, but I'm not too sure, I'm very new to this software.

One other question, and this is embarrassing as I literally just got this code from this website and inserted it into my form, but what is the difference between having "Me!" and "Me." ?

Code:
Private Sub Form_Current()
 If Me.Parts_On_Order.Value = -1 Then
  Me!PO.Enabled = False
  Me!Parts_ETA.Enabled = False
  Me!Parts_Notes.Enabled = False
 Else
  Me!PO.Enabled = True
  Me!Parts_ETA.Enabled = True
  Me!Parts_Notes.Enabled = True
End If
End Sub

Private Sub Parts_On_Order_Click()
 If Me!Parts_On_Order.Value = True Then
  Me!PO.Enabled = True
  Me!Parts_ETA.Enabled = True
  Me!Parts_Notes.Enabled = True
 Else
  Me!PO.Enabled = False
  Me!Parts_ETA.Enabled = False
  Me!Parts_Notes.Enabled = False
  Me!PO.Value = Null
  Me!Parts_ETA.Value = Null
  Me!Parts_Notes.Value = Null
End If
End Sub

Private Sub Parts_Required_Click()
 If Me!Parts_Required.Value = True Then
  Me!Part_Numbers_Description.Enabled = True
  Me!Parts_Required_By.Enabled = True
 Else
  Me!Part_Numbers_Description.Enabled = False
  Me!Parts_Required_By.Enabled = False
  Me!Part_Numbers_Description.Value = Null
  Me!Parts_Required_By.Value = Null
End If
End Sub

Thanks for any help you can provide,
~FOX~
 
Well if i am reading your post right then...
  1. In the Form_Open event include some code to enable the textbox (that is if the checkbox is always going to be selected) OR code to set the checkbox to selected and enable the textbox.
  2. Make the code you have in Parts_On_Order_Click() a seperate procedure which you can then call from Parts_On_Order_Click() and Form Current()
  3. Me! and Me. As far as i know there is no difference. I use Me. when refering to controls/properties etc on a form. I use the exclamation when refering to an object collection (EG Forms!SomeForm) as it removes the need to use brackets or quote marks. I am sure some of our more knowledgeable members will answer this more clearly:D
 
There are subtle differences between the dot (.) and bang (!) operator. This article will help you understand these differences.
 
JBB thanks for that link. Whilst i generally use the dot and bang as suggested in the article, it is always good to know why:D
 
Yeah, I don't think I explained that well enough now that I read it again.
So, I've got a check box that when selected enables a text box. In my example I've entered information into a new record, so I've checked my check box, which enabled my text box and I entered in some information into that text box. Now I closed my form. Two days later I open my form again and looked at the previous record I created and the check box IS selected (as I expected it would be) and there IS information in the text box (again, as I expected to see) however the text box is disabled...or NOT enabled and as a result is greyed out.
The problem with this is that if I wanted to add some more information to that text box, I can't, I can only do it when I deselect and reselect the check box. The problem with that is I have it set so that when the check box is deselected, the information in the text box is cleared...which is how I want it to stay.
So what I'm looking for is that when the form opens it displays the text box (either enabled or disabled) relative to the status of the check box.

I hope that makes sense, if not let me know and I'll have another go at it.

Cheers,

~FOX~
 
The code in Form_Current does the exact opposite of what happens in Parts_On_Order_Click. The True's and False' are opposite.

I think what you want is:

Code:
Private Sub Form_Current()
 If Me!Parts_On_Order Then
  Me.PO.Enabled = True
  Me.Parts_ETA.Enabled = True
  Me.Parts_Notes.Enabled = True
 Else
  Me.PO.Enabled = False
  Me.Parts_ETA.Enabled = False
  Me.Parts_Notes.Enabled = False
End If
End Sub

or just:

Code:
Private Sub Form_Current()
  Me.PO.Enabled = Me!Parts_On_Order 
  Me.Parts_ETA.Enabled = Me!Parts_On_Order 
  Me.Parts_Notes.Enabled = Me!Parts_On_Order 
End Sub
 
Hi VilaRestal,

Thanks for your help!! I've amended my code to read:

Private Sub Form_Current()
Me.PO.Enabled = Me.Parts_On_Order
Me.Parts_ETA.Enabled = Me.Parts_On_Order
Me.Parts_Notes.Enabled = Me.Parts_On_Order
Me.Part_Numbers_Description.Enabled = Me.Parts_Required
Me.Parts_Required_By.Enabled = Me.Parts_Required
End Sub


Private Sub Parts_On_Order_Click()
If Me!Parts_On_Order.Value = True Then
Me!PO.Enabled = True
Me!Parts_ETA.Enabled = True
Me!Parts_Notes.Enabled = True
Else
Me!PO.Enabled = False
Me!Parts_ETA.Enabled = False
Me!Parts_Notes.Enabled = False
Me!PO.Value = Null
Me!Parts_ETA.Value = Null
Me!Parts_Notes.Value = Null
End If
End Sub

Private Sub Parts_Required_Click()
If Me!Parts_Required.Value = True Then
Me!Part_Numbers_Description.Enabled = True
Me!Parts_Required_By.Enabled = True
Else
Me!Part_Numbers_Description.Enabled = False
Me!Parts_Required_By.Enabled = False
Me!Part_Numbers_Description.Value = Null
Me!Parts_Required_By.Value = Null
End If
End Sub

And everything works as it should. Thank you SO much!!

~FOX~
 

Users who are viewing this thread

Back
Top Bottom