Answered by:
checkbox query error when migrating backend from Access to SQL

-
text/html 6/21/2013 3:58:34 PM Carl_S_S 0
I have a database that I'm working on migrating the backend from Access to SQL. I have a form that we use to choose projects from. There's the user's name (assigned IE), a check box (ckCompletedProjects) for open or completed projects and the project name (ProjName). There's a query that has these 3 fields in with one being filtered by the checkbox ([Forms]![fmIE's]![ckCompleteProjects]). This would then filter what's seen in the Projects drop down menu. This worked fine with the Access backend, but now that I've moved it to SQL, it shows no results for when the box is checked. Below is the SQL query.
SELECT ProjectInfo.ProjName, ProjectInfo.[Assigned IE], ProjectInfo.[Completed?]
FROM ProjectInfo
WHERE (((ProjectInfo.[Assigned IE]) Like [Forms]![fmIE's]![AssignedIE] & "*") AND ((ProjectInfo.[Completed?])=[Forms]![fmIE's]![ckCompleteProjects]));With the Access backend, the field was a checkbox, but now that it's SQL the field is either 0 or -1. How do I convert the check to a -1?
- Changed type Carl_S_S Thursday, June 27, 2013 4:01 PM choose incorrectly
Question
Answers
-
text/html 6/21/2013 5:10:41 PM Andrey Artemyev 0
You missed ( after Abs.
Yes, I know about about 0 and -1, that's why I just offer to try. Interesting, I have hundreds hours of Access + SQL Server interaction, but now I understand that I have never faced this situation. :) But since the query returns no records, this means some wrong value in WHERE clause is.Try to check what all you controls return.
Andrey V Artemyev | Saint-Petersburg, Russia
- Marked as answer by Carl_S_S Tuesday, December 10, 2013 3:45 PM
All replies
-
text/html 6/21/2013 4:02:33 PM Andrey Artemyev 1The field was always 0 or -1, it was just represented as a checkbox. The difference here, I think, that bit datatype in SQL Server may be null, 0 or 1, not -1. So, try ProjectInfo.[Completed?])=Abs([Forms]![fmIE's]![ckCompleteProjects])
Andrey V Artemyev | Saint-Petersburg, Russia
- Marked as answer by Dummy yoyoModerator Tuesday, July 09, 2013 2:14 AM
- Unmarked as answer by Dummy yoyoModerator Tuesday, July 09, 2013 2:43 AM
-
text/html 6/21/2013 4:47:18 PM Carl_S_S 0
I'm assuming you mean to place that in the Where statement. If so, Access doesn't like it. I get a Syntex error (missiong operator) in query expression '(((ProjectInfo.[Assigned IE]) Like [Forms]![fmIE's]![AssignedIE] & "*") AND ((ProjectInfo.[Completed?])=Abs[Forms]![fmIE's]![ckCompleteProjects])'
but just for the record, when I open the table in Access, it shows me 0's and -1's in the field for Completed.
-
text/html 6/21/2013 5:10:41 PM Andrey Artemyev 0
You missed ( after Abs.
Yes, I know about about 0 and -1, that's why I just offer to try. Interesting, I have hundreds hours of Access + SQL Server interaction, but now I understand that I have never faced this situation. :) But since the query returns no records, this means some wrong value in WHERE clause is.Try to check what all you controls return.
Andrey V Artemyev | Saint-Petersburg, Russia
- Marked as answer by Carl_S_S Tuesday, December 10, 2013 3:45 PM
-
text/html 6/21/2013 6:17:49 PM Carl_S_S 0
I figured out how to get around the issue, still don't know exactly why the issue is occurring. I went into the query and hard coded 0 for the criteria of Completed. ran the query and it returned the active projects. I then hard coded -1, ran the query and returned no results (expected). I then hard coded 1 and ran the query and it returned all the completed projects and showed the results of -1 in the Completed field.
So to get around the issue I've added an additional field to the query for the check box on the form. in the criteria, I have 0 or -1 (not checked or checked). In the Completed field, I have 0 or "-1 or 1".
-
text/html 6/21/2013 6:28:43 PM Andrey Artemyev 0So, finally does it work with 1? If so, Abs() does the same, converts -1 to 1.
Andrey V Artemyev | Saint-Petersburg, Russia
- Marked as answer by Dummy yoyoModerator Tuesday, July 09, 2013 2:13 AM
- Unmarked as answer by Dummy yoyoModerator Tuesday, July 09, 2013 2:14 AM
-
text/html 6/21/2013 7:14:30 PM Alphonse G 1
Threre should not be a problem. I have plenty of check boxes bount to SQL Server bit fields. Access handles the conversion nicely.
What version of SQL Server?
Is the query based on an ODBC linked table?
What ODBC driver are you using?
Make sure that the field in SQL Server is a BIT, NOT NULL with default value of 0.
-
text/html 6/21/2013 7:35:07 PM Carl_S_S 0
-
text/html 6/21/2013 7:36:58 PM Andrey Artemyev 0
-
text/html 6/21/2013 7:40:47 PM Carl_S_S 0not 100% sure what version of SQL. Someone else set it up for me and gave me a database on it. Yes the query is based on and ODBC linked table. I'm using whatever version of ODBC that comes with Windows 7. And I did check the table and the field is a BIT.
-
text/html 6/21/2013 7:59:08 PM Alphonse G 1
Cosidering that you don't know what you are dealing with, and it sounds like the back end was simply converted to SQL Server, my guess is that, unless your application is very simple, you will be running into more problems.
I suggest you do some investigation and research. Here is a good place to start. http://msdn.microsoft.com/en-us/library/bb188204.aspx
-
text/html 6/22/2013 12:41:26 AM Van Dinh 0
>>And I did check the table and the field is a BIT.<<
Did you check this using the Linked Table in Access or the Table in SQL Server Mangement Studio?
If you open the Design View of the Linked Table, the Field [Completed?] (more on the custom names later) should show the data-type Boolean (or one of its aliases such as "Yes/No Field", "True/False") and definitely not BIT.
I suspect that you checked the Linked Table since if the Linked Table shows BIT, you have the problem exactly as described in this thread.
You (or someone with perms to modify the SQL Server Table) need to change the Field [Completed?] in the SQL Server Table to BIT, Not Null and set the DefaultValue to 0 or 1 (depending on whether you want the Default Value in Access as False or True). Once you re-link the Table, Access will recognize this Field as a Boolean Field and you can use the reference to the CheckBox directly as per the SQL in your first post.
While we are on the SQL Server / Linked Tables, do you have a TimeStamp Field in each of the SQL Server (user) Tables being used as the sources for the Linked Tables in your Access Front-End? You need this TimeStamp Field so that your Access Front-End can work efficiently with SQL Server Tables being used as Linked Tables as well as avoiding "Write conflict" errors that can surface later.
Finally, avoid using illegal names and reserved words for your the custom names in your database, e.g. "Completed", "fmIE's", etc... While they work fine if you take care to remember to use the name delimiter [...], it is not the complications you need. If you use only legal names, you can omit the name delimiter in most (if not all) cases.
P.S. The new MSDN interface is a bit confusing but what you posted is definitely "Ask a Question" and not "Start a discussion" when you created the thread. Please select appropriately in future threads.
Van Dinh
- Edited by Van DinhMVP Monday, June 24, 2013 7:33 AM Typos
-
text/html 6/27/2013 4:05:27 PM Carl_S_S 0
-
text/html 6/27/2013 4:15:20 PM Carl_S_S 0
Thanks Van Dinh.
I had admin rights to the database, and used a tool called Toad. Looking at the table thru it, I could see that the datatype is BIT, and the default value 0. Looking at the table in Design View, it shows it to be Yes/No.
The tables do all have a timestamp field.
I've taught myself Access, and don't have anyone at my company (that I know of) that I can go to for direction. Where would you suggest I go to learn more about the illegal and reserved words?
You are correct, the new MSDN interface is confusing. I intended on starting this as an "Ask a Question", must have missed that somewhere. I figured out how to change it and did.- Edited by Carl_S_S Thursday, June 27, 2013 4:16 PM added info
-
text/html 6/27/2013 9:50:11 PM Van Dinh 0
1. Did you solve the original problem with using the CheckBox Control from your Form to supply the Boolean value to your Query criteria?
2. For reserved words, See Allen Browne's Problem names and reserved words in Access.
Van Dinh
- Edited by Van DinhMVP Thursday, June 27, 2013 10:25 PM Addendum
-
text/html 6/30/2013 3:48:28 AM Albert D. Kallal 0
Keep in mind that Access true/false columns are NOT -1 and 0.
I also STRONGLY suggest you don't cast the value using abs() as that would convert the value form true/false into a interger.
You also don't want to use some kluge since there might be 100's of other places in your application where this could become an issue and as a result cause MUCH pain and suffering here.
So keep in mind that while SQL server does in fact using a VERY small integer column (1 bit!!!), it is not a true/false column but simply only a integer column.
In access, we DO USE a real true/false. And in our quires we use
Where ActiveInvoice = True
We as a general rule do NOT use -1 or 0 in our code nor our quires. (at least I hope we don't, since this suggests one is casting a integer value of -1 or 0 into true/false.
The MOST important concept here is the ODBC driver will convert the bit (integer) into a CORRECT Access True/false column. (assuming the data was up-sized correctly in the first place).
So the first thing to check is to ensure that the Access linked table sees this column as true/false. Open up the linked table in design view FROM Access (ignore the message about this being a linked table).
Check (and then check again) to ensure that the column in question is defined as a Yes/No column. If it is a integer or some such then I STRONGLY suggest you had a issue during the up-sizing and I would convert this to a bit column, and then re-link the Access client.
And while the linked tabled DOES show -1 and 0, you will in fact see that ALL REGULAR Access tables will do the same ****IF**** you REMOVE the format in the non linked table.
So in table desing (linked or not) you can check the "lookup" setting and it will be set to plain text box or check box (in fact this how you change the default column from showing a check box as opposed to a text box with -1/0). Note also the format option to choose on/off or yes/no or true/false. If you remove this setting – you get -1, 0 (and do so even with local tables).
So if you remove the formatting in Access (all cases linked or not) then we see -1 or 0 but you STILL WANT to use True and False in your querires. Best way to think of this just like interal date format vs external – Access has a internal Boolean format and it is NOT -1 and 0 but true/false.
So Access linked tables to SQL server are NOT any different. The only real surprise is that a linked table by default has NO formatting set and the display "check box" is turned off.
In fact you CAN open up the linked table in design view and again ignoring the linked table message in fact add format to that column and save it (and even change the plain text box in lookup to check box). The result will be a table that display JUST like any regular non-linked Access table.
However, you don't need nor should have to change the format as per above.
The base issue here is your check boxes on the form are likely un-bound (or bound to a wrong data type column that was messed up during the up-size.
The fix here is to CAST the check box into a REAL AND CORRECT Boolean value.
This should work:
= cbool([Forms]![fmIE's]![ckCompleteProjects])
Note how we force the check box to return a true/false value.
So cast (convert) the check box into a true false (and ALSO remember to set the default for this check box when the form loads to False. (again don't use 0, but false).
You REALLY want to make sure you have this correct else in a large up-size project you will have a huge mess and can introduce 100's of bugs that you will chase down for a long time.
It is possible that the original table in Access was not a yes/no and was a integer – I would check this out, but if that was the case then case the check box using cint( ) as opposed to cbool()
So "cast" the check boxes into a correct true/false and this should work.
And in your query always use True/False – even if no formatting you will see a (-1/0) but you might see "true/false" or "on/off" or "yes/no" depending on the format setting, but in all cases we still shold use true/false just like we use USA format for dates.
Best regards,
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada -
text/html 7/2/2013 3:25:39 PM Carl_S_S 0Albert, thanks for all your good info, however after following it I still couldn't get the query to work. I checked the original table and the field Format was set to On/Off with field Lookup being Checkbox. I changed it to True/False, dropped the table from SQL and re-uploaded it. I then went to my front end, opened the table in design view and noticed that the formatting wasn't there, I set it as described above. I now see check boxes instead of 0's and -1's when viewing the data in the table. I removed the ABS in the query, changed to cbool, but no change in the results of the query (I get results if the check box is unchecked, but no results when the check box is checked). I then tried changing it to CINT, but again no results when checked. The only thing that works so far is ABS.
-
text/html 7/2/2013 4:03:53 PM Van Dinh 0
1. Did you check the data-type of the Field of the Field in the SQL Server Table using SSMS (SQL Server Management Studio)? Is Null allowed on this Field?
2. Open the DEsignView of the Linked Table and ignore the Format of the Field, what data-type does Access assign to this Field?
See the graphic on the place to look for the data-type as senn by Access. The Field to look at is ActiveStatus. You can also see that I don't bother with the Format Property of the Field.
Van Dinh
- Edited by Van DinhMVP Tuesday, July 02, 2013 4:11 PM Clarification
-
text/html 7/3/2013 5:30:59 AM Albert D. Kallal 0
Just keep in mind that the ONLY thing suggested to change here is to write the control with cbool(). However you should not have required to do this in the first place.
I think something else is being overlooked here. So there should be no cint() etc. in the query but ONLY the cbool() wrapped around the control (forms!) ref you have in that query.
As noted you also want to check if the column does show up as a Yes/No column as Van suggests. However the fact that you were able to change this to a check box suggests your column is correctly as a yes/no.
Off the top of my head - this query should have worked without issue and without any required changes.
I thinking that the check box must have some kind of default value that then means the un-bound check box is not a yes/no but is number or some such. And perhaps those check boxes are NOT unbound on the form – if that's the case, then again double check what column data type they are bound to.
Best regards,
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada -
text/html 7/3/2013 2:05:06 PM Carl_S_S 0
-
text/html 7/3/2013 2:16:41 PM Van Dinh 0
1. Just to be sure, get the person who created the SQL Server database for you to check that the Field [Completed?] on the SQL Server is of bit data-type, Null not allowed and a default value is set (presumably 0 since at the start of the project, it is not yet completed).
2. Open your Form in Design view and check that the Property "Triple State" is set to No/False. You may want to set the Property Default Value of the CheckBox to either False or True also).
Van Dinh
-
text/html 7/3/2013 3:38:38 PM Carl_S_S 0
-
text/html 7/3/2013 4:05:52 PM Van Dinh 0
Try:
1. Open your Form, check the CheckBox, move the Focus to another Control then use the Immediate/Debug window to check its value with
? Forms("YourForm").YourCheckBox
This should return True or -1.
Run your Query (using simply the full ref to the CheckBox as the criterion) and check whether it returns the completed Projects (allowing for the other condition also).
2. Uncheck the CheckBox, move the focus to another Control and run the same statement in the Immediate window again. This time, it should return False or 0.
Run your Query again and check whether it returns the current Projects (allowing for the other condition condition also).
Please post the results of the 2 tests here...
Van Dinh
-
text/html 7/3/2013 7:12:36 PM Carl_S_S 0
-
text/html 7/3/2013 11:26:23 PM Van Dinh 0
-
text/html 7/9/2013 7:59:53 PM Carl_S_S 0
-
text/html 7/9/2013 8:28:36 PM Carl_S_S 0
-
text/html 7/9/2013 11:30:28 PM Van Dinh 0
-
text/html 7/9/2013 11:34:38 PM Van Dinh 0
Does the field have to be BIT for yes/no, on/off, etc? The guy who installed SQL for me had me change the field to INT, and now it works without the ABS.
Did you re-link the Table before testing?
I always use SQL Server bit not Null with default value for ACE Boolean Field? I had problems using int before so I have not used int as the equivalent of ACE Boolean for a long time.
Van Dinh
-
text/html 7/9/2013 11:46:13 PM Alphonse G 0
-
text/html 7/10/2013 12:52:59 AM Van Dinh 0
>>There has to be something missing here...<<
I have at agree with that... So far in the thread, I have confirmed that:
* the Field was correctly set in SQL Server (bit not Null with Default value)
* the Table does have a TimeStamp Field
* the Linked Table is re-linked when appropriate
* The Field in the Linked Table is correctly show as Boolean Field
So both the SQL Server Table and the Access Linked Table are correct, AFAIK. However, the criterion using the CheckBox (tri-state = No) Control reference still does not work as per earlier in the thread.
The problem is what this "something" is...
Van Dinh
-
text/html 7/10/2013 1:11:02 AM Alphonse G 0
-
text/html 7/10/2013 2:56:06 AM AccessVandal 0
Carl_S_S,
Late again as usual, lol.
Here’s what you can do assuming that others had not asked you to do the changes when you upsized it into SQL Server.
Use the built-in function to convert the "-1" value to a proper value to match the "bit" value in SQL server.
IIf([Forms]![fmIE's]![ckCompleteProjects], 1,0)
By default, Access reads 0 as False and…….. -1,1,2,3…. as True.
So….your SQL string should read like this…..
ckCompleteProjects = False 'for 0 in SQL Server
ckCompleteProjects = True 'For 1 in SQL Server
The above is the correct string syntax if you want it to work but the value of "-1" will fail.
The problem with Access Checkbox that it will read the value of "True" as "-1" default by design which of cause, would not work in SQL Server. But it has no problem with "0".
If you have set the Checkbox default value in the properties, make sure it’s not the value of "-1". Set it to "1" instead if it’s possible and if you can.
- Edited by AccessVandal Wednesday, July 10, 2013 3:22 AM typo
-
text/html 7/10/2013 8:47:07 PM Carl_S_S 0
-
text/html 7/10/2013 8:54:02 PM Carl_S_S 0
AccessVandal, thanks for the info, but I'm a little lost. Since Access uses -1, not 1, why would we want to change all the -1's to 1's? And where would I enter "IIf([Forms]![fmIE's]![ckCompleteProjects], 1,0)"?
As for setting the default answer to 1, that would be the opposite of what I want. A 1 or -1 would represent a closed project. I need the default answer to be 0 representing an open project. In my query, if I change it to say that 1 equals completed, the query works, but the problem is that this is incorrect. It should be -1. If Access didn't upload the table correctly (any of the times that I've done this), could I enter IIf([Forms]![fmIE's]![ckCompleteProjects], -1,0) somewhere and convert all the 1's to -1? -
text/html 7/10/2013 8:58:53 PM Alphonse G 0
Change your driver! Use SQL Server Native Client 10.0. 11.0 is actually newer but I haven't used it yet.
Download driver from
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=ceb4346f-657f-4d28-83f5-aae0c5c83d52
(expand Install Instructions).Look under the section 'SQL Server® 2008 R2 Native Client'.
Use x86 if your OS is 32bit & x64 if it is 64bit or IA if appropriate.
-
text/sourcefragment 7/11/2013 1:44:53 AM AccessVandal 0
AccessVandal, thanks for the info, but I'm a little lost. Since Access uses -1, not 1, why would we want to change all the -1's to 1's? And where would I enter "IIf([Forms]![fmIE's]![ckCompleteProjects], 1,0)"?
As for setting the default answer to 1, that would be the opposite of what I want. A 1 or -1 would represent a closed project. I need the default answer to be 0 representing an open project. In my query, if I change it to say that 1 equals completed, the query works, but the problem is that this is incorrect. It should be -1. If Access didn't upload the table correctly (any of the times that I've done this), could I enter IIf([Forms]![fmIE's]![ckCompleteProjects], -1,0) somewhere and convert all the 1's to -1?As I have mentioned, Access reads "-1", "1", "2"....so on....... as "True". This means the checkbox is "Checked" and the value from the Checkbox output value is "-1". It's usually the case, but it also defends on what you had modified on the checkbox that the output value will show "True".
I assumed the code I gave is "True".
If you want the value to be "False" (for searching), set it to validate the output of the Checkbox....like
Iff([Forms]![frmIE's][ckCompleteProjects] = False,1,0) '----Assuming it was reversed on server side
Just reversed the order of "1" and "0". I assumed that the server side of the "bit" was reversed as well?
Iff([Forms]![frmIE's][ckCompleteProjects] = False,0,1) '......like this?
You can set the default to 0 if you or that's what you intend to do. I'm just stating the fact you might want to set it to 1. (I was mistaken that you put the default to "-1").
You can also set the default value in the properties to "True" or "False" as well if you like. (Note: without the quotes)
By default, I do mean the control Checkbox "ckCompleteProjects", not on the server side of the Table setting. You don't alter anything when you upsized the Tables to SQL Server. The default is "0" in the upsized tables.
The built-in function is not converting value at all. It just converting it to a "bit" value in your SQL String so that Access can read it correctly or correcting the SQL syntax.
An alternative is...
Iff([Forms]![frmIE's][ckCompleteProjects],False,True)
or
Iff([Forms]![frmIE's][ckCompleteProjects],"False","True")
The one with quotes is just in case you need it as a string for your SQL syntax correction.
PS. In Addition.....
I notice your sql string might give you some problem....
WHERE (((ProjectInfo.[Assigned IE]) Like [Forms]![fmIE's]![AssignedIE] & "*")
The above is not correct as you have missed out the quotes. It should be like...
WHERE (((ProjectInfo.[Assigned IE]) Like " & """" & [Forms]![fmIE's]![AssignedIE] & ""*)""
Next...
& " AND ((ProjectInfo.[Completed?])= " & IIf([Forms]![fmIE's]![ckCompleteProjects],1,0) & "));"
or
& " AND ((ProjectInfo.[Completed?])= " & IIf([Forms]![fmIE's]![ckCompleteProjects],True,False) & "));"
- Edited by AccessVandal Friday, July 12, 2013 1:34 AM typo