Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding DateTime to CheckBox and updating DateTime field upon CheckBox click 1

Status
Not open for further replies.

Rajesh Karunakaran

Programmer
Sep 29, 2016
535
0
16
MU
Hi friends,

I have DateTime field in a table. I have a grid in a form where I show this table. In a column, I need to show CheckBoxes where it will be checked if DateTime field is not empty or unchecked if otherwise. Besides, upon clicking on CheckBox, I need to update the DateTime field with current date-time.

Can we really do this? Any idea?

Thank you all in advance.
Rajesh
 
Hi Rajesh,

Yes, we can definitely do this. But in trying to answer your question, I don't know where to start.

Are you asking:

- How to place a checkbox in a grid?

- How to set the checkbox state (checked or unchecked) according to the value of the datetime?

- How to set the datetime when the checkbox is clicked?

All of those are possible. But before trying to answer all three, can you be more specific in what you want to know?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
OK, I'll make a start on the above list, until I run out of time (or until someone else gives you all the answers you need).

First:

How to add a checkbox to a grid

1. In the Form Designer, right-click on the grid, and choose Edit.

2. Click in the column (the column itself, not the header) that is to contain the checkbox. You should now see the name of that column in the drop-down list at the top of the Properties window.

3. Select a checkbox in the toolbar, Project Manager window, Toolbox, or whatever. Drop it in the column.

Nothing seems to have happened. In fact, the checkbox has been successfully added, but it is obscured by the existing textbox, which is still present. (You can confirm this by looking at the drop-down list at the top of the Properties window.)

So, the next step is to remove the textbox:

4. Select the textbox in the drop-down list at the top of the Properties window. Be sure that it's the textbox in the column which also has the checkbox.

4. Click on the title bar of the Form Designer (not on the form itself; if you do, you will de-select the textbox). Press the Del key.

You should now see the checkbox in the column. If you can't see them, set the column's Sparse property to .f.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike,

Mike said:
- How to set the checkbox state (checked or unchecked) according to the value of the datetime?

- How to set the datetime when the checkbox is clicked?

These are my queries. In this, if I set the control source of the checkBox column to something like "(IIF(!EMPTY(tbl_emailin.workon), .T., .F.))", I get the ticks wherever there is date. This is okay.

But, that creates a problem when checkBox is clicked to save current Datetime to the field.

Thanks
 
How to set the checkbox state

You need to set the value of the underlying logical field in the table or cursor that populates the grid. So the first step is to set the ControlSource of the checkbox's column to point to that field.

At the point at which you populate the grid, you can set the value of the logical field (in every row) like so:

[tt]REPLACE ALL TheLogicalField WITH NOT EMPTY(TheDateTimeField)[/tt]

If you want to do it interactively, that is, when the user sets a value in the datetime, repeat the above code, but omit the word ALL. That will make the change for the current row only. (You might need to SetFocus to the grid in order to see the change.)

And finallly:

How to set the datetime when the checkbox is clicked

This is easy. Just put this code in the checkbox's Click event:

[tt]REPLACE TheDateTimeField WITH DateTime()[/tt]

And again SetFocus to make the change visible.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
"(IIF(!EMPTY(tbl_emailin.workon), .T., .F.))"

This is unecessarily complicated. Instead of [tt]"(IIF(!EMPTY(tbl_emailin.workon), .T., .F.))"[/tt], just use [tt]"NOT EMPTY(TheDateTimeField)".[/tt]

But that's not the point. Instead of setting the ControlSource to that expression, you should set it to the alias.fieldname of the logical field in the underlying table. If you do that, you will be able to set the datetime from the Click event, as I showed above.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I followed half the discussion, enough to know you don't have a logical field in the table, but want the checkbox to be checked when a date exists and unchecked when empty, plus having the functionality to set the date when checked.

Setting controlsource to an expression is one-way, i.e. makes the checkbox read-only.

What you need is an extra field that can be bound two-way in the grid. This extra field doesn't need to become a permanent field of the table, it can be added and set with the expression Select ..., !EMPTY(workon) as checked, ... From tbl_emailin and bind the grid to that.

The interactivechange then can set the datetime field, that would be the idea in short.

You could also have a separate alias with just the checkbox state and set a relation from tbl_emailin to it, so in the grid accessing the checked field is as if this field was part of tbl_emailin.


Chriss
 
Dear Mike,

I think, I had not explained it well. In fact, I don't have a logical field for the checkBox but only a Datetime field. I wanted the checkbox to be checked in the date is there, otherwise it will be unchecked. Also, if I click on checkbox I want the current datetime to be updated in the date field. This is the need.

Chriss has catched that point :).

Dear Chriss,
An alternate alias is better option it seems. I also was thinking in the same way.
The client table is a free table. I am binding that table directly to the grid.
However, need to handle for multi-user in this case (the table being a free one where buffering etc not available)

Thanks.
 
Hi Rajesh,

Rajesh said:
The client table is a free table. I am binding that table directly to the grid.
That's okey, but not bothering me at all. Introducing a logical field for the checkbox in the grid can be done with anything without needing to modify itself with both a query that adds the field into a grid cursor and with an extra alias related 1:1.

However, need to handle for multi-user in this case (the table being a free one where buffering etc not available)
Buffering is available for free tables, locking, etc. too. What's not available for free tables among other things is long field names and default values for fields, a primary key - but a candidate does for that matter. But you can include free tables in transactions, for example - see MAKETRANSACTABLE), you can have modern fields (a free VFP table still is a VFP table, not legacy pre 2.6 table) or you can of course buffer them and have locks on them, these things are part of the workarea, not table files. So anything that a workarea can use can be buffered and locked and thus can have all things related to multi user needs.

What is unfortunate with a logical field that is part of the backend table is the usual case, that user selections are something local to each user session and you don't want to set a logical "selected" flag affecting the selections on another users session.

In your case a logical field in the backend table would work, though, when it's checked and becomes true and the datetime is set, so for any other user it should also be true and checked. Also vice versa, so different users should all see the same state of the checkbox, no matter if they checked it or another user. This check data is really sharable. The only thing that makes it undesirable to have it permanently in the backend table is that it's redundant. It's just !EMPTY(datefield) and so the datefield already stores that and an extra field could become a contradiction to it, which is the main reason to avoid redundant data.



Chriss
 
Hello,

I think you can add a checkbox with an expression and put code in the click event of the checkbox like this :
IF EMPTY(tabelle1.dt)
REPLACE tabelle1.dt WITH DATE()
thisform.grid1.Refresh()
ENDIF

I added a little testform

Regards
tom
 
 https://files.engineering.com/getfile.aspx?folder=4acb5fb9-81b2-4317-a4e7-b85ada5b791a&file=gridcheckbox.rar
It works, wouldn't have thought the click event runs.
But it has that ugly sound and message "The control is read-only".

Chriss
 
Dear Chriss and all others,

I created a separate cursor to hold the status of presence of a date in Datetime field, set up a relation to original table and now it works fine.

Thank you,
Rajesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top