Results 1 to 3 of 3

Thread: Using VB to connect to MS Access

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Location
    In the woods
    Posts
    4

    Question Using VB to connect to MS Access

    The following code runs as compiled code outside of MS Access. I'm trying to filter a recordset on the Access form. I can get a handle to the form object as an AccessObject, but when I try to set the object to an Access Form object I get an Error 13, Type Mismatch. I can retrieve a filtered recordset, but I can't apply this to the current recordset of the form. Any help would be greatly appreciated. The whole VB project can be emailed for debugging if that would help.
    Thanks!

    Public Sub FindRecord()

    Dim access_app As Access.Application
    Dim db As CurrentProject
    Dim cnn1 As New ADODB.Connection
    Dim comm As New ADODB.Command
    Dim rs As New ADODB.Recordset
    Dim rs_filtered As New ADODB.Recordset
    Dim rs_temp As New ADODB.Recordset
    Dim db_name As String
    Dim obj As AccessObject
    Dim frm_obj As AccessObject
    Dim frm As Access.Form
    Dim frm_coll As Collection
    Dim frm_name As String
    Dim ndx As Long
    Dim cnt As Long
    Dim frm_title As String
    Dim str As String
    Dim SPN_txt As String
    Dim prop As Property

    SPN_txt = "235128301001"
    frm_name = "f_Parcels"

    Set access_app = Application
    Set db = access_app.CurrentProject

    db_name = db.Name
    MsgBox "The open database is " & db_name

    For Each obj In db.AllForms
    'MsgBox "The object name is " & obj.Name
    If obj.Name = frm_name Then
    MsgBox "The desired object name is " & obj.Name
    Set frm_obj = obj
    Exit For
    End If
    Next

    '-------------------------------------------------
    'This is where the type mismatch happens
    Set frm = frm_obj
    frm_title = frm.Name
    MsgBox "The title of the form is " & frm_title
    '--------------------------------------------------

    Set cnn1 = DataEnvironment1.Connection_parcel
    Set comm = DataEnvironment1.Commands("parcel_att")
    'MsgBox "cnn1 state: " & GetState(cnn1.State)

    Set rs = comm.Execute
    Set rs_temp = rs
    rs_temp.Filter = "SPN = '" & SPN_txt & "'"

    Set rs_filtered = rs_temp

    cnt = rs_filtered.RecordCount
    MsgBox "The filtered record count is " & cnt

    End Sub

  2. #2
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: Using VB to connect to MS Access

    Welcome to the forums TedW
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  3. #3
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Thumbs up Re: Using VB to connect to MS Access

    Quote Originally Posted by TedW
    '-------------------------------------------------
    'This is where the type mismatch happens
    Set frm = frm_obj
    frm_title = frm.Name
    MsgBox "The title of the form is " & frm_title
    '--------------------------------------------------
    Try this
    VB Code:
    1. Set frm = new frm_obj

Posting Permissions

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



Click Here to Expand Forum to Full Width