How to check checkbox depending on string

PoorCadaver

Registered User.
Local time
Today, 04:15
Joined
Oct 25, 2011
Messages
30
Hi!

I want to make a checkbox do something like this:

IF strlen(notes) >= 1 THEN check1 = true

notes=column with notes

If there is something written in the column "notes" then I want to check the checkbox. It doesn't feel too difficult, but I just can't figure it out!

Some one here who can help me?

/PoorCadaver
 
Do you mean something written in the same record, or in any record (since you said column)? Presuming the former, this would be a bit of a normalization mistake (calculated column), but in code it would look like:

IF len(Me.notes & vbNullString) > 0 THEN Me.check1 = true
 
Bit more explanation, and alternatives


First, Paul's syntax may appear strange to you.
the reason for the syntax is because a simple

if len(me.notes)>0 then

will produce a runtime error, if notes is actually NULL, as opposed to a zero length string. It's impossible to tell the difference between a NULL and a zls by inspection. Nulls are ALWAYS a bit of a pain in access, and you often have to allow for the possiblity of a NULL.

so all of these

IF len(Me.notes & vbNullString) > 0 THEN Me.check1 = true
IF len(nz(Me.notes,"") > 0 THEN Me.check1 = true
IF nz(Me.notes,"")<>"" THEN Me.check1 = true

do the same thing. I think paul's version is the most efficient, because of the way strings are stored.

Note that you ALSO need to allow for a false result, otherwise you won't be able to clear a checked checkbox, so you end up with this

IF len(Me.notes & vbNullString) > 0 THEN
Me.check1 = true
else
me.check1 = false
end if

which can be written more concisely (but less intuitively) as

me.check1 = len(Me.notes & vbNullString) > 0

----
note that instead of doing it this way, you can simply put the control source of the check box to be

= (len(me.notes & vbNullString) > 0 )

doing it this way is "pulling" the value in, doing it your way is "pushing" it in. either way is OK.


Finally note that the me. qualifier is generally not necessary in this example.
 
Hi!

I don't get it to work. The string
IF len(Me.notes & vbNullString) > 0 THEN Me.check1 = true
where should I put it? I tried Detail and the checkbox but it didn't work.
 
You will need to put the code in the After Update event of the notes field, and also the On Current event of the form.
 
But there is no notes field in the form, I 'm using the checkbox instead to indicate that there is something written about the record.
 
But there is no notes field in the form, I 'm using the checkbox instead to indicate that there is something written about the record.

Your code reads;
Code:
IF len(Me.[B]notes[/B] & vbNullString) > 0 THEN Me.check1 = true
So what is that field you are checking the Len() of :confused:
 
There is a notes field in the table, but I don't show it on the form, and I'm not supposed to either.

I tried to add and hide a textbox connected to Notes, but it didn't work. I have added the If Len() - code at Form_Current and Notes_AfterUpdate, but it didn't work.
 
Last edited:
Please see my edit on my previous post, I just edited it when you answered =)

The notes textbox I made shows the values correctly. Isn't it the same thing?
 
Did you adjust the names in code of the notes text box and the checkbox to match the names of yours?
 

Users who are viewing this thread

Back
Top Bottom