?
Solved

how to populate a checkbox from an access database (Data Type Yes/No) in vb 2008

Posted on 2013-01-27
9
Medium Priority
?
451 Views
Last Modified: 2013-01-30
I'm using the following code to get and display the Yes/No data type value from an Access database. This is the error I'm getting:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "Veteran" to type 'Integer' is not valid.

How would I convert the string to an integer so the checkbox on my form can display a check symbol if the value is true?

Veteran.CheckState = OleDbReader.GetValue("Veteran")
0
Comment
Question by:rfgraham
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 750 total points
ID: 38825709
Hi....
Try to use an if/then Clause deppending on your Value
If [YourDatabase Value]="Yes" then Veteran.CheckState=CheckState.Checked
0
 
LVL 16

Expert Comment

by:kmslogic
ID: 38825829
I think there's something wrong with your reading of the yes/no field.  It sounds like what you're getting with OleDbReader.GetValue("Veteran") is the string "Veteran" and you should be getting a 0 or 1 (maybe -1 I forget) integer value from that query.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38826346
You can change your query so it returns 1 or 0 instead of yes/no

Select IIF(fieldname = 'Yes', 1, 0) From TableName
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 28

Assisted Solution

by:Ark
Ark earned 750 total points
ID: 38829476
Veteran.Checked = OleDbReader.GetValue("Veteran").Equals("Veteran")
0
 

Author Comment

by:rfgraham
ID: 38831157
Using:
Veteran.Checked = OleDbReader.GetValue("Veteran").Equals("Veteran")
Is still returning the error: Conversion from string "Veteran" to type 'Integer' is not valid.
The value being returned from the query is {0} so I'm not sure if the check box on the form is requiring a text value or not. If it is then the integer needs to be converted to a string value. Any idea of how that would be done?

Thanks
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38831175
How about the other 2 solutions?
0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 750 total points
ID: 38831880
If i  remember well the field in access is Yes/No..
Did you try
 
If OleDbReader.GetValue("Veteran")="Yes" then Veteran.CheckState=CheckState.Checked

Open in new window

0
 
LVL 28

Accepted Solution

by:
Ark earned 750 total points
ID: 38833823
Oops, sorry - OleDBDatareader.GetValue require column's ordinal, not its name. So, use
If OleDbReader.GetValue(3) 'where 3 is "Veteran" column's ordinal
0
 

Author Comment

by:rfgraham
ID: 38838373
I came up with this solution and it works fine
If cboxVeteran.Checked <> OleDbReader("Veteran") Then cboxVeteran.CheckState = CheckState.Checked _
                    Else cboxVeteran.CheckState = CheckState.Unchecked

Thanks for all your help
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question