RunTime Error 13......Mismatch

  • Thread starter Thread starter orsonros
  • Start date Start date
O

orsonros

Hi!

I have written the following code to create a RECORDSET . Whenever I
load the program it gives me a mismatch error. I like to know why this
error occurs, and what remedial action is to be taken. Are any setting
to be done?

The database is Trad1.mdb & table tblMain

Help needed

Thanks

ORS












Option Compare Database
Option Explicit
Dim MainWS As Workspace, MainDb As Database
Dim rstMain As Recordset


Private Sub Form_Load()
On Error GoTo Error_Handler

Set MainWS = DBEngine.Workspaces(0)
Set MainDb = DBEngine.OpenDatabase("c:\Trad1.mdb")

' This will create a table-type Recordset.
Set rstMain = MainDb.OpenRecordset("tblMain", dbOpenTable)


rstMain.Index = "TID"

rstMain.MoveLast
ShowData



Exit_Here:
Exit Sub


Error_Handler:
MsgBox Err.Number & ":" & Err.Description
Resume Exit_Here

End Sub

'
 
Both the DAO and ADO libraries have a Recordset object. The "type mismatch"
error suggests you are getting the wrong one.

Try specifying the type you want:
Dim rstMain As DAO.Recordset

More info about libraries, and which ones you need for each version of
Access:
http://allenbrowne.com/ser-38.html
 
Thanks Allen,

The "Type mismatch error" has gone, but now on click event of cmdAdd it
does not take me to a new record! why?

Ors
 
Option Compare Database
Option Explicit
Dim MainWS As Workspace, MainDb As Database
Dim rstMain As DAO.Recordset
'Declare this logical variable
Dim bAddNew As Boolean


Private Sub cmdAdd_Click()

bAddNew = True

rstMain.MoveLast
rstMain.AddNew

End Sub

This the code for adding new record. It does not take me to a new
record.

Ors
 
1. You do not need to do MoveLast, before you do Addnew.

2. Doing an AddNew is pointless, unless you subsequently put some
values into the fields of the new recordset entry, and then use the
..Update method to save the new record into the database. Otherwise, the
unsaved new record is discarded automatically.

3. None of the above will have any affect on the records *displayed in
your form*.

4. Why are you doing this anway? Access *automatcally* lets you add,
edit & delete records, via a form, without writing any code at all.

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
Hi! TC,

You asked why am I doing it this way.

Well I had written it the same way for a Visual Basic program and it
did well, so I thought maybe it should work in Access as well.

Am I right or do the access coding have differences?

I don't know!!!

Ors
 
Access has the concept of "bound forms". You can create forms that let
the user find, view, add, edit, and delete records, *without any
programming*. I'm no VB expert, but I don't think that VB has bound
forms. So clearly, you approach the job quite differently, when using
Access, compared to VB.

In general, there's no guarantee that a particular programming method
that works with VB, will work correctly - or at all - with Access. In
particular, the code that you have shown so far - taken exactly as you
have written it - will not work in Access, *or VB*.

I suggest that you forget that code, and research the use of "bound
forms" in Access. In particular, check ut the form-level RecordSource
property, and the control-level ControlSource property, in F1 Help.

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
Back
Top