Results 1 to 3 of 3

Thread: connect Visual Basic and Access 2003

  1. #1
    Member
    Join Date
    Sep 2007
    Posts
    51

    Question connect Visual Basic and Access 2003

    Hey,

    I am trying to access a MS Access 2003 database from a Visual Basic project. I have written the following code in a module of the project. I call the dbOpen() function from some form code and then on another event I call the setAttracRecord() function, which causes an error.

    Code:
    Imports System.Data.OleDb
    
    Module mdlDbConnect
        Private strConnect As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=database.mdb;Persist Security Info=False"
        Private objConnect As New OleDbConnection(strConnect)
    
        Sub dbOpen()
            objConnect.Open()
        End Sub
    
        Sub setAttracRecord(ByVal s As String)
            Dim strSQL As String = "SELECT * FROM tblAttrac"
            Dim cmd As New OleDbCommand(strSQL, objConnect)
            Dim dtRdr As OleDbDataReader
            dtRdr = cmd.ExecuteReader
            If dtRdr.HasRows Then
                MsgBox("Reader DOES have rows.")
            Else
                MsgBox("Reader does NOT have rows.")
            End If
    
            'error occurs on next line
            Dim strTest As String = dtRdr.GetString(0)
            MsgBox("Returned:" & strTest)
        End Sub
    
    End Module
    I edited the code to try and track down the problem. When I execute the code, the msgbox saying "Reader DOES have rows." is displayed and then the GetString() method causes and error. I get the debug message "No data exists for the row/column.".

    I've tried to figure out this problem, on my own, but I've now run out of ideas. Because as far as I can tell dtRdr does have data in it, as HasRows() method returns true, but the debug message is telling me that it doesn't. Therefore, if anyone could help, I would very much appreciate it.


    Thanks in advance,

    - user0108

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Location
    Texas
    Posts
    168
    Try this

    dtRdr.Read()
    Dim strTest As String = dtRdr.GetString(0)

    Edit:
    You need to read in data before you can parse it out using GetString()
    Last edited by Darksnake; March 30th, 2008 at 06:05 AM.
    <chsh> I've read more interesting technical discussion on the wall of a public bathroom than I have at AO at times

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Darksnake's correct - you need to read a row before you can access the data. Quite a common thing you might want to do is go through all the rows like so:

    Code:
    while(reader.HasRows())
    {
      reader.Read();
    
      // get some data and use it
    }
    One thing you have to be careful of (at least in SQL databases - I don't know about Access) is that you can have problems reading null values. So you might want to call reader.IsDBNull(x) (where x is the column) before using Getxxx().

    ac

    [edit]Bear in mind I use C# so the code above is probably not correct in VB[/edit]

Similar Threads

  1. Port List
    By ThePreacher in forum Miscellaneous Security Discussions
    Replies: 17
    Last Post: December 14th, 2006, 09:37 PM
  2. Basic Unix security tutorial
    By \/IP3R in forum AntiOnline's General Chit Chat
    Replies: 16
    Last Post: March 7th, 2005, 10:25 PM
  3. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 08:02 AM
  4. Newbies, list of many words definitions.
    By -DaRK-RaiDeR- in forum Newbie Security Questions
    Replies: 9
    Last Post: December 14th, 2002, 08:38 PM
  5. The Worlds Longest Thread!
    By Noble Hamlet in forum AntiOnline's General Chit Chat
    Replies: 1100
    Last Post: March 17th, 2002, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •