Getting data into a form from Access database

Posted on 2010-08-17
Last Modified: 2012-05-10

Would this code work? I am trying to count the number of "A" entered in the Response field.
the database is called Survey.
Dim number As Integer
        Dim con As OleDbConnection
        Dim comm As OleDbCommand
        Dim reader As OleDbDataReader
        con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Survey.accdb;")
        comm = New OleDbCommand("Select count(Response)as number from Answers where SurveyID = 1 and QuestionID =1 and Response = 'A'", con)
        ' Open Connection
        ' reader read comm
        reader = comm.ExecuteReader
        ' Get from the reader the UserName and UserPasswrod
        While reader.Read
            number = reader.Item("Response")

        End While
Question by:adamtrask
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1

Expert Comment

ID: 33458069
Because you are using Count this should only return one value, and you don't need execute reader, what is the error you are getting?
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33458075
add a space before "as"

comm = New OleDbCommand("Select count(Response) as number from Answers where SurveyID = 1 and QuestionID =1 and Response = 'A'", con)

or in access vba, just not sure if it will work on .net

comm = New OleDbCommand("Select count("*") as number from Answers where SurveyID = 1 and QuestionID =1 and Response = 'A'", con)

Author Comment

ID: 33458078
To tell you the truth I haven't tried it yet....
I'll do that now and get back to you
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Expert Comment

ID: 33458121
or you can try this...

 Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\survey.accdb;Persist Security Info=False"
        Dim cmd As String = "Select Count(response) as iCount from Answers Where surveyID=1 and questionID=1 nad Response='A'"
        Dim adapter As New OleDbDataAdapter(cmd, conn)
        Dim rs As New DataSet

rs.Count as well as rs("iCount") should tell you the total number of people who chose a.

Author Comment

ID: 33458441
This the error i am getting

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect
LVL 10

Expert Comment

by:Jini Jose
ID: 33458506
there is a spelling mistake in your code. change the nad to and before Response='A'"

Author Comment

ID: 33458708

It looks like it's working. The thing is I don't know how to extract the value contained in "rs".

Is it possible to do some thing like:
dim count as integer = rs.value....???
LVL 10

Expert Comment

by:Jini Jose
ID: 33458793
check the below link. you will get the basic idea

LVL 10

Expert Comment

by:Jini Jose
ID: 33458806
you can get the values as below



Author Comment

ID: 33458974
I get the following error:

Fields is not a member of system.Data.Dataset
LVL 10

Expert Comment

by:Jini Jose
ID: 33459057
sorry I posted the vb6 codes.

Accepted Solution

adamtrask earned 0 total points
ID: 33461125
Thanks to every one who responded. I found the solution using the ExecuteScalar method which executes the SQL statement associated with the Command object and returns a single value. So in the case of the code above it would be some thing like that:

Dim Number as integer
comm = New OleDbCommand("Select count(Response)from Answers where SurveyID = 1 and QuestionID =1 and Response = 'A'", con)
Number =comm.ExecuteScalar

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating a View from a CTE 15 39
Subtract dates in 6 30
Enable TLS 1.2 for SQL 2012 Web Edition 1 20
Creating Scalar Function 3 14
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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