Error adding variables to old code (2 Viewers)

Ilovexfiles

Registered User.
Local time
Today, 01:54
Joined
Jun 27, 2017
Messages
37
I have just added "Option Explicit" and am trying to add variables to old code.

In my codes I am referring to "field names" (example below).
I added variables on the top of my module such as:

Dim PreExistingProgram as Integer

This caused compile problems in all of my modules, with errors stating:

"The member already exists in an object module from which this object module derives"
Any help understanding this is appreciated.

Private Sub PreExistingProgram_AfterUpdate()
If PreExistingProgram.Text = "none" Then
PreExistingAgency.Enabled = False
PreExistingProgram2.Enabled = False
PreExistingAgency2.Enabled = False

Else
PreExistingAgency.Enabled = True
PreExistingProgram2.Enabled = True
PreExistingAgency2.Enabled = True
End If
End Sub
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:54
Joined
Jul 9, 2003
Messages
16,403
What if you rename it?

Change This;-
Dim PreExistingProgram as Integer

To This:-
Dim PreExistingProgramX as Integer
 

Ilovexfiles

Registered User.
Local time
Today, 01:54
Joined
Jun 27, 2017
Messages
37
What if you rename it?

Change This;-
Dim PreExistingProgram as Integer

To This:-
Dim PreExistingProgramX as Integer

I know this may sound silly, but I have been reading about declaring variables but am not fully understanding it yet.

My old code I created was implicit, and referenced fields on a form, and worked effectively. I am not sure how to reference a new name such as "PreExistingProgramX" and have still reference my field on my form that the code is in called "preExistingprogram"

Do I need to declare variable for this type of code?
 

isladogs

MVP / VIP
Local time
Today, 09:54
Joined
Jan 14, 2017
Messages
18,287
Going back to post 1, you got errors because you already have that variable in use elsewhere, possibly as a string or whatever.
So Access is telling you there's a clash.

In the visual basic editor, do a search for PreExistingProgram & see what you get.

Then in the new location, use a different variable name
E.g Dim UncleGizmo As integer
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 09:54
Joined
Jan 14, 2017
Messages
18,287
I know this may sound silly, but I have been reading about declaring variables but am not fully understanding it yet.

My old code I created was implicit, and referenced fields on a form, and worked effectively. I am not sure how to reference a new name such as "PreExistingProgramX" and have still reference my field on my form that the code is in called "preExistingprogram"

Do I need to declare variable for this type of code?

BTW there's no such thing as implicit code
 

Adam Caramon

Registered User
Local time
Today, 04:54
Joined
Jan 23, 2008
Messages
822
I have just added "Option Explicit" and am trying to add variables to old code.

In my codes I am referring to "field names" (example below).
I added variables on the top of my module such as:

Dim PreExistingProgram as Integer

To be clear, the variable and the controls are different things. To help keep these separate, use a prefix. For example, instead of

Dim PreExistingProgram as Integer

use

Dim intPreExistingProgram as Integer.

Instead of PreExistingProgram for the control name, prefix what this actually is (i.e., if it is a text box, it would be txtPreExisitingProgram, if it is a combo box, cboPreExistingProgram, etc.)

This will save you a lot of headaches as you continue to work with your code into the future.
 
Last edited:

Ilovexfiles

Registered User.
Local time
Today, 01:54
Joined
Jun 27, 2017
Messages
37
To be clear, the variable and the fields are different things. To help keep these separate, use a prefix. For example, instead of

Dim PreExistingProgram as Integer

use

Dim intPreExistingProgram as Integer.

Instead of PreExistingProgram for the object name, prefix what this actually is (i.e., if it is a text box, it would be txtPreExisitingProgram, if it is a combo box, cboPreExistingProgram, etc.)

This will save you a lot of headaches as you continue to work with your code into the future.

Thanks, when I change the name Access says it cannot find what I am referring to. How do I associate the name, say cboPreExistingProgram to the field that is on the form itself?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:54
Joined
Jul 9, 2003
Messages
16,403
What type of control is:-

"PreExistingProgram"

This being it's AfterUpdate event:-
Private Sub PreExistingProgram_AfterUpdate()
 

Adam Caramon

Registered User
Local time
Today, 04:54
Joined
Jan 23, 2008
Messages
822
Thanks, when I change the name Access says it cannot find what I am referring to. How do I associate the name, say cboPreExistingProgram to the field that is on the form itself?

You rename the control on the form (i.e., change the combo box name from PreExisitingProgram to cboPreExisitingProgram.) Then look in the events of the control (After Update, for example.) If there is no value in the field, click the ellipsis button, choose Code Builder, then click OK. The control's event would then be associated with the corresponding code (i.e. Private Sub cboPreExisitingProgram_AfterUpdate)
 

Ilovexfiles

Registered User.
Local time
Today, 01:54
Joined
Jun 27, 2017
Messages
37
What type of control is:-

"PreExistingProgram"

This being it's AfterUpdate event:-
Private Sub PreExistingProgram_AfterUpdate()

It is a bound control (associated with field of same name which is a dropdown in the table)
...I hope I am explaining this correctly :)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:54
Joined
Jul 9, 2003
Messages
16,403
Adam is sort of anticipating my line of reasoning in assuming that your control is a text box. I was asking you what type of control it was, with a view to explaining using a prefix "txt" I'm also concerned because you are referring to the textbox contents with the keyword "Text" you should really be using the word value. You can only use "Text" if the control has the focus... See:-

https://msdn.microsoft.com/VBA/Access-VBA/articles/textbox-text-property-access

Note To set or return a control's Text property, the control must have the focus, or an error occurs.

In most instances you're going to be using the word "Value", however as "Value" is the default you don't necessarily need to use that word. However I would caution against leaving value out even though it is the default because I have seen a couple of instances where there were problems by omitting value. I've been meaning to blog about "Value" I am currently gathering information.

You might occasionally need to use the word "Text" if you want to capture what the person has entered into the control before the value is saved.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:54
Joined
Jul 9, 2003
Messages
16,403
It is a bound control (associated with field of same name which is a dropdown in the table)
...I hope I am explaining this correctly :)

I should have been more specific in my question. I meant is it a textbox, combobox some other type of control that you would find on a form?
 

Adam Caramon

Registered User
Local time
Today, 04:54
Joined
Jan 23, 2008
Messages
822
It is a bound control (associated with field of same name which is a dropdown in the table)
...I hope I am explaining this correctly :)

Okay - the control has a "Name" property and a "Control Source" property.

You change the "Name" property by adding the prefix to it. Don't change the "Control Source" property.
 

Ilovexfiles

Registered User.
Local time
Today, 01:54
Joined
Jun 27, 2017
Messages
37
I changed the Control Names to different names than my actual field names.

Now I get this error: Object Variable or With Block Error Not Set

The goal is to grey out other controls (apple, orange and grapes) when "None" is selected for Control "cboPre"



Private Sub cboPre_AfterUpdate()
Dim cboPre As ComboBox
Dim cboApple As ComboBox
Dim cboOrange As ComboBox
Dim cboGrapes As ComboBox

Set cboPre = Me!cboPre

If cboPre.Value = "None" Then
cboApple.Enabled = False
cboOrange.Enabled = False
cboGrapes.Enabled = False
Else
cboApple.Enabled = True
cboOrange.Enabled = True
cboGrapes.Enabled = True

End If
End Sub
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:54
Joined
Jul 9, 2003
Messages
16,403
I changed the Control Names to different names than my actual field names.

Now I get this error: Object Variable or With Block Error Not Set

The goal is to grey out other controls (apple, orange and grapes) when "None" is selected for Control "cboPre"



Private Sub cboPre_AfterUpdate()
Dim cboPre As ComboBox
Dim cboApple As ComboBox
Dim cboOrange As ComboBox
Dim cboGrapes As ComboBox

Set cboPre = Me!cboPre

If cboPre.Value = "None" Then
cboApple.Enabled = False
cboOrange.Enabled = False
cboGrapes.Enabled = False
Else
cboApple.Enabled = True
cboOrange.Enabled = True
cboGrapes.Enabled = True

End If
End Sub

Do you actually have these combo boxes on your form?

cboApple
cboOrange
cboGrapes

If you don't have them then that could well be the problem.
 

Ilovexfiles

Registered User.
Local time
Today, 01:54
Joined
Jun 27, 2017
Messages
37
Do you actually have these combo boxes on your form?

cboApple
cboOrange
cboGrapes

If you don't have them then that could well be the problem.

Yes, I have these Combo Boxes on the form, and I changed their names to the above

The problem is highlighted after Else
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:54
Joined
Jul 9, 2003
Messages
16,403
Comment out the three lines as shown:-

'Dim cboApple As ComboBox
'Dim cboOrange As ComboBox
'Dim cboGrapes As ComboBox

Code:
Private Sub cboPre_AfterUpdate()
    Dim cboPre As ComboBox
    'Dim cboApple As ComboBox
    'Dim cboOrange As ComboBox
    'Dim cboGrapes As ComboBox
    
    Set cboPre = Me!cboPre
    
If cboPre.Value = "None" Then
        cboApple.Enabled = False
        cboOrange.Enabled = False
        cboGrapes.Enabled = False
Else
        cboApple.Enabled = True
        cboOrange.Enabled = True
        cboGrapes.Enabled = True
      
End If
End Sub

Then add three real combo boxes to the form.
cboApple
cboOrange
cboGrapes

If you do that then your code will work...
 

isladogs

MVP / VIP
Local time
Today, 09:54
Joined
Jan 14, 2017
Messages
18,287
As was explained earlier by someone else, don't define combo boxes or other controls.

Remove the 4 dim and use actual comboboxes

The Set line should also be removed
Me.cboPre is what it is. It doesn't need to be set

Also I would recommend you use the syntax Me. as follows

Me.cboApples.Enabled=True ... etc
 

Users who are viewing this thread

Top Bottom