Subform problem with VBA code execution

IanMilly

Registered User.
Local time
Today, 21:43
Joined
Jun 1, 2004
Messages
46
hi

i have a db with several forms and subforms. I have creatd this db from an old db i was making (development version if you like)

I have a form with a subform inserted. On the subform are several check boxes. When Checkbox 1 is true i want Checkbox 2 to be false (and vice versa), I have built an event as such
Code:
Private Sub HD_Click()
    If HD.Value = True Then
        LD.Value = False
    End If
End Sub

Hd is Checkbox1, Ld is checkbox2 (this code is repeated twice , but switched, for the other option).

The code works fine for my normal form, but in a subform an error occurs. Is there something about the code when implimenting a subform that causes the error?


I also have a image swap item which shows a picture according to a combobox option (A,B or unknown) and the two checkboxes (LD and HD)(once the button "Swap_Image" is pressed). The image name is "swap". This code works fine in a form, but when it is used as a subform problems occur.

Code:
Private Sub Swap_Image_Click()
If ((Config.Value = "A") And (LD = True)) Then
     Swap.Picture = "C:\Documents and Settings\user\My Documents\My Pictures\Configs\LD_ConfigA_Internal_Power.gif"
Else
    If ((Config.Value = "A") And (HD = True)) Then
         Swap.Picture = "C:\Documents and Settings\user\My Documents\My Pictures\Configs\HD_ConfigA_Internal_power.gif"
    Else
        If ((Config.Value = "B") And (LD = True)) Then
             Swap.Picture = "C:\Documents and Settings\user\My Documents\My Pictures\Configs\LD_ConfigB_External_power.gif"
        Else
            If ((Config.Value = "B") And (HD = True)) Then
                 Swap.Picture = "C:\Documents and Settings\user\My Documents\My Pictures\Configs\HD_ConfigB_External_power.gif"
            Else
               If Config.Value = "UNSELECTED" Then
                  Swap.Picture = "C:\Documents and Settings\user\My Documents\My Pictures\configs\jumpersettings.gif"
               End If
            End If
        End If
    End If
End If
End Sub

As I said the code works fine in my old db in a normal form, but when used in the new db in a subform an error occurs. Anyone have a solution to this problem? :confused: The error that appears is attached

Help is very much appreciated
 

Attachments

  • error.GIF
    error.GIF
    41.9 KB · Views: 271
Last edited:
I would use Me.Config rather than Config.Value to reference the Config control. Both will work but the first syntax gives you intellisense.

If the config field is on the parent form rather than on the sub form use -
Me.Parent.Config
 
Thanks for the reply,

i relaixsed this morning about the Me. methid, however this was not the problem.


I had two different problems.

The one with the config file (a picture to be displayed is selected depending on 3 variables) i believe was an actual error with access and the way it linked to the code, I made an exact same copy of the form and used the same code, it worked like a dream (i did redo with me. later for better coding :) )

On another form with lots of checkboxes:
I copied the form from the original db, the code was attached to the form, however i had to build every event again for each checkbox individually and the code worked fine. A bit strange the code was not being recognised before the rebuild.

Thanks for the help, i will use the me. format for its "intellisense" in the future - I have never really used VB before having only programmed in C++ and Java, I take it that "me" is similar to "this".

Thanks again

Ian
 
When Checkbox 1 is true i want Checkbox 2 to be false (and vice versa), I have built an event as such

Just a suggestion: Option buttons will work in the same way and you don't even have to write code for them.

what you do is create a Option group and use the wizard to do the rest (it will create as many option buttons that you put in)

Hope this helps
 
I am getting exactly the error in the first point - code that works in the mainform is bombing in the subform

I have tried it with an On Dirty and and On Load and it doesnt matter what the VBA is, I get the error in IanMilly's gif - even when the only code in the VBA sub is 'test

What I am actually trying to achieve is:

- two buttons (Cancel & Confirm) to be disabled when the form loads [this code works fine on the mainform, but if i move it on the subform and modify the control references, it errors]
- in the subform, On Dirty the two buttons get enabled
- and then code on the buttons to either save the record or me.undo
 
You are probably not referencing the fields properly -

To reference a field on the current form:
Me.SomeField
To reference a field on the parent form from a subform:
Me.Parent.SomeField
To reference a field on a subform from the parent form:
Me.subform.Field!SomeField

Don't forget that when referencing a subform from its parent form, you are referencing ONLY the Current record so make sure you understand which record is current.
 

Users who are viewing this thread

Back
Top Bottom