setting default value of txtbox in code

Me won't work when using a form as a subform. Me references the current form and the main form is really considered the current form so the subform can't use it.
 
> Me won't work when using a form as a subform. Me references the current form and the main form is really considered the current form so the subform can't use it.

Are you really sure?

Within a form or reports class module, I thought that Me referred to that particular form or report, regardless of how the form or report was used.
 
> Me won't work when using a form as a subform. Me references the current form and the main form is really considered the current form so the subform can't use it.

Are you really sure?

Within a form or reports class module, I thought that Me referred to that particular form or report, regardless of how the form or report was used.

Absolutely positive - been a pain in the butt for me for a long time. You can create the subform by itself and use Me. for things and they work. The minute you put it into a main form, BAM! it generates an error.

I used to use the fully qualified Forms!MainFormName.SubformContainerName.Control syntax, but I stumbled upon the fact that it actually works if you just reference the control, without a qualifier (simplifies coding, for sure).
 
I'm sorry, I do not believe this is correct. Me refers to the class (here form class or report class) regardless of how it's used. If things errors up, then there must be other things playing up than the Me keyword.

See for instance the reference you just put into the Access FAQ section, which correctly recognizes " Me!ControlName" as a valid way of referencing a control on a subform from the same subform.

But, it would be nice to see this problem of yours in a db.
 
Absolutely positive - been a pain in the butt for me for a long time. You can create the subform by itself and use Me. for things and they work. The minute you put it into a main form, BAM! it generates an error.

I used to use the fully qualified Forms!MainFormName.SubformContainerName.Control syntax, but I stumbled upon the fact that it actually works if you just reference the control, without a qualifier (simplifies coding, for sure).

ahhhhhh......explains a lot.

You know, I haven't had anything but trouble with this database since I tried to get fancy and put subforms inside the main form...

It's mind boggling and frustrating. You read the help screens, you read your reference books and get nothing. google search is like looking for a needle in a haystack.

Glad I found this forum.

thanks.
 
I downloaded your db, and found that you are setting the default value two places.

In the Forms After update event, where you did it without quotes:

Me.txtOrderNumber.DefaultValue = Me.txtOrderNumber

and in the On Lost Focus event of the control, where you did

Const cquote = """"
Me.txtOrderNumber.DefaultValue = cquote & Me.txtOrderNumber & cquote

Now, this last one is correct (but I would use the after update event of the control), but since the after update event of the form occurs after the on lost focus of the control, the correct default value is overwritten by the incorrect assignement in the forms after update event, giving #Name (or something else) as result.

That was the problem, not the referencing style.

Dropping in the same base where I've commented the form after update and the controls on lost focus, and added the following to the controls after update
Code:
Private Sub txtOrderNumber_AfterUpdate()

    If (Me!txtOrderNumber.Value <> Me!txtOrderNumber.DefaultValue) Then
        Me!txtOrderNumber.DefaultValue = Chr(34) & Me!txtOrderNumber.Value & Chr(34)
    End If

End Sub
 

Attachments

I downloaded your db, and found that you are setting the default value two places.

In the Forms After update event, where you did it without quotes:

Me.txtOrderNumber.DefaultValue = Me.txtOrderNumber

and in the On Lost Focus event of the control, where you did

Const cquote = """"
Me.txtOrderNumber.DefaultValue = cquote & Me.txtOrderNumber & cquote

Now, this last one is correct (but I would use the after update event of the control), but since the after update event of the form occurs after the on lost focus of the control, the correct default value is overwritten by the incorrect assignement in the forms after update event, giving #Name (or something else) as result.

That was the problem, not the referencing style.

Dropping in the same base where I've commented the form after update and the controls on lost focus, and added the following to the controls after update
Code:
Private Sub txtOrderNumber_AfterUpdate()

    If (Me!txtOrderNumber.Value <> Me!txtOrderNumber.DefaultValue) Then
        Me!txtOrderNumber.DefaultValue = Chr(34) & Me!txtOrderNumber.Value & Chr(34)
    End If

End Sub

can't explain it as the lost focus code was written first and only after that did not work did I move to different code in the after update event.
 
can you get at the default value in the forms fields collection then bob

something like

"formname".controls!control("fieldname").defaultvalue

might be close
 
gemma,

since this is a subform, you cannot reference like that, because a subform is not a member of the forms collection, but a control on the main form on which it resides. You do mean controls, don't you? A form doesn't contain a fields collection. You can reach the forms controls collection within it's class module through for instance using the Me keyword, i e

Me!txtOrderNumber.DefaultValue

for each ctl in me.controls
debug.print ctl.defaultvalue
next ctl

or fully qualified reference, going through the main form, like for instance

forms!frmMaintenanceEntries!sfmParts.Form!txtOrderNumber.DefaultValue,
forms("frmMaintenanceEntries").Controls("sfmParts").Form.Controls("txtOrderNumber").DefaultValue

for each ctl in forms("frmMaintenanceEntries").Controls("sfmParts").Controls
debug.print ctl.defaultvalue
next ctl

or similar, though I would always prefer the Me keyword when working within a class module. I think that when coding in a class module, using a fully qualified reference, at least to me, means you're referring "outside the class (form)", and then back to the same class module. I e,

Me!txtSomeControl

in stead of

forms!frmMain!frmSub.Form!txtSomeControl

Sometimes, especialy when doing automation, a coding style like that might leave you an extra instance of the automated application in memory.

For some info, check out the article boblarsson put in the faq section, or perhaps http://support.microsoft.com/kb/209099

You can also use the

Form_FormName.txtMyControl

syntax, which some claims work flawlessly with subforms too, but you'll run a chance of instantiating/opening it as a form, if it isn't alredy open, either as a single form, or as part of a main/sub form setup.

Did I understand your question?
 

Users who are viewing this thread

Back
Top Bottom