473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Type mismatch

94
I am getting the error message 'type mismatch' at line 22 when running the code below:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2. 'On Error GoTo Err_cmdFacilitatorSave_Click
  3.  
  4.     'Check flag status
  5.     If Me!txtFlag = 1 Then  '(New)
  6.         '----------------------------------------------------------------------------------------------------
  7.         MsgBox "New Facilitator"
  8.         Me!txtID.SetFocus
  9.         'Check to make sure the user has entered a facilitator ID
  10.         If Me!txtID.Text = "" Then 'If they haven't.....
  11.             MsgBox "You must ensure that you enter a facilitator ID (usually initials)."
  12.             Me!txtID.SetFocus
  13.         Else                       'If they have.....
  14.             Dim dbsCTrack
  15.             Dim rstNewFacil As Recordset
  16.             Dim sqlNewFacil As String
  17.  
  18.             MsgBox Me.txtID
  19.  
  20.             Set dbsCTrack = CurrentDb
  21.             sqlNewFacil = "SELECT * FROM tblFacilitator WHERE tblFacilitator.facil_id = '" & Me.txtID & "'"
  22.             Set rstNewFacil = dbsCTrack.OpenRecordset(sqlNewFacil)
  23.  
  24.             MsgBox "Test"
  25.  
  26.             'Check to see if the facilitator already exists
  27.             If rstNewFacil.RecordCount = 0 Then    'If they don't.....
  28.                 rstNewFacil.AddNew
  29.                 rstNewFacil(0) = Me.txtID
  30.                 rstNewFacil(1) = Me.txtName
  31.                 rstNewFacil(2) = Me.txtAdd1
  32.                 rstNewFacil(3) = Me.txtAdd2
  33.                 rstNewFacil(4) = Me.txtAdd3
  34.                 rstNewFacil(5) = Me.txtAdd4
  35.                 rstNewFacil(6) = Me.txtPcode
  36.                 rstNewFacil(7) = Me.txtPhone
  37.                 rstNewFacil(8) = Me.txtEmail
  38.                 rstNewFacil.Update
  39.                 rstNewFacil.Close
  40.                 MsgBox Me.txtID & " - " & Me.txtName & " has been created as a facilitator."
  41.             Else    'If they do.....
  42.                 MsgBox "A facilitator already exists with the ID: " & Me!txtID & ". Please enter a different ID."
  43.                 Me!txtID.SetFocus
  44.             End If
  45.         End If
  46.     ElseIf Me!txtFlag = 2 Then '(Edit)
  47. 'If statement continues.....
It is worth noting that the field txtID is just a plain text box with an input mask that allows a maximum of 3 characters but no less that 2.
I can't understand it.... I've even replaced the SQL at line 21 and hard coded a two character ID such as....
Expand|Select|Wrap|Line Numbers
  1. sqlNewFacil = "SELECT * FROM tblFacilitator WHERE tblFacilitator.facil_id = 'FT'"
but i still get the same error message. I don't know what i'm doing wrong as i've definately done something like this before.

Any help would be much appreciated!!
May 8 '08 #1
5 1310
FishVal
2,653 Expert 2GB
Helo, Lewe22.

Try explicitely declare your variables using libnames.
Expand|Select|Wrap|Line Numbers
  1. Dim dbsCTrack As DAO.Database
  2. Dim rstNewFacil As DAO.Recordset
  3. Dim sqlNewFacil As String
  4.  
Regards,
Fish
May 8 '08 #2
NeoPa
32,556 Expert Mod 16PB
As a rule (very strong recommendation) you should always "Require Variable Declaration" in any VBA projects (From VBA Window {Alt-F11 from Access} use Tools / Options / Editor Tab / Require Variable Declaration).

This will insert "Option Explicit" automatically into all of your project modules. This will make the system complain whenever you use a variable that you haven't declared in the code. This may sound like it will cause you extra work. That would be a woeful misunderstanding!

Moving on to Recordsets in particular. As there are many overlaps between the two libraries (ADO & DAO) it is advisable to be explicit in any declarations to avoid ambiguity. Both for you and the compiler.
May 8 '08 #3
Lewe22
94
Thanks for the help FishVal - much appreciated, that sorted it out for me!

Thanks for the advice NeoPa - Will ensure I select the 'Require Variable Declaration' as you kindly suggest.

Great Stuff!
May 8 '08 #4
NeoPa
32,556 Expert Mod 16PB
As this came up in here, and I think it's such an important issue anyway, I knocked up a little article that explains it in more detail (Require Variable Declaration).
May 8 '08 #5
ADezii
8,834 Expert 8TB
'A slightly different approach with Explicit Declarations
Expand|Select|Wrap|Line Numbers
  1. 'On Error GoTo Err_cmdFacilitatorSave_Click
  2.  
  3. 'Check flag status
  4. If Me!txtFlag = 1 Then  '(New)
  5.   '------------------------------------------
  6.   MsgBox "New Facilitator"
  7.   Me!txtID.SetFocus
  8.     'Check to make sure the user has entered a facilitator ID
  9.     If Me!txtID.Text = "" Then 'If they haven't.....
  10.       MsgBox "You must ensure that you enter a facilitator ID (usually initials)."
  11.       Me!txtID.SetFocus
  12.     Else                       'If they have.....
  13.       Dim dbsCTrack As DAO.Database
  14.       Dim rstNewFacil As DAO.Recordset
  15.       Dim sqlNewFacil As String
  16.  
  17.       MsgBox Me.txtID
  18.  
  19.       Set dbsCTrack = CurrentDb
  20.       sqlNewFacil = "SELECT * FROM tblFacilitator WHERE tblFacilitator.facil_id = '" & _
  21.                      Me.txtID & "'"
  22.       Set rstNewFacil = dbsCTrack.OpenRecordset(sqlNewFacil)
  23.  
  24.       MsgBox "Test"
  25.  
  26.       'Check to see if the facilitator already exists
  27.       If rstNewFacil.RecordCount = 0 Then    'If they don't.....
  28.         With rstNewFacil
  29.           .AddNew
  30.             .Fields(0) = Me.txtID
  31.             .Fields(1) = Me.txtName
  32.             .Fields(2) = Me.txtAdd1
  33.             .Fields(3) = Me.txtAdd2
  34.             .Fields(4) = Me.txtAdd3
  35.             .Fields(5) = Me.txtAdd4
  36.             .Fields(6) = Me.txtPcode
  37.             .Fields(7) = Me.txtPhone
  38.             .Fields(8) = Me.txtEmail
  39.           .Update
  40.         End With
  41.            rstNewFacil.Close
  42.            Set rstNewFacil = Nothing
  43.            MsgBox Me.txtID & " - " & Me.txtName & " has been created as a facilitator."
  44.       Else    'If they do.....
  45.         MsgBox "A facilitator already exists with the ID: " & Me!txtID & _
  46.                ". Please enter a different ID."
  47.         Me!txtID.SetFocus
  48.       End If
  49.     End If
  50. ElseIf Me!txtFlag = 2 Then '(Edit)
  51. 'If statement continues.....
May 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Arun Wadhawan | last post by:
Hello MY SQL Server is causing me this problem : Microsoft VBScript runtime error '800a000d' Type mismatch: 'ident' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I am getting from...
1
by: LJgrnl | last post by:
I've got a type mismatch error that's driving me nutty. Variable blnNoData has the initial value False. If a recordset comes back empty (both .EOF and ..BOF are true) then blnNoData is set to...
1
by: Mark | last post by:
Hi - I tried this in VS.Net, and also in the Web Matrix code below: - but I am getting a type mismatch error. The sql statement runs perfectly from within the Access Query Designer. Can anyone...
7
by: middletree | last post by:
I've been messing with this for hours, and have been to various sites, including Aaron's site, and am truly stumped. The short version: in SQL Server, the 4 fields in question are datetime. I...
4
by: Mike | last post by:
I am getting a type mismatch error when I do a bulk insert. ---Begin Error Msg--- Server: Msg 4864, Level 16, State 1, Line 1 Bulk insert data conversion error (type mismatch) for row 1, column...
3
by: amitbadgi | last post by:
I am getting teh following error while converting an asp application to asp.net, Exception Details: System.Runtime.InteropServices.COMException: Type mismatch. Source Error: Line...
1
by: Brett | last post by:
I have a form that calls a method within a DLL. By clicking a button on the form, the DLL is instantiated and the SaveOutlookMessage() method invoked. The DLL code copies messages from Outlook to...
6
by: Howard Kaikow | last post by:
I'm doing a VB 6 project in which I am trying to protect against type mismatch errors. Is the process any different in VB .NET? Here's what I'm doing in VB 6. I have an ActiveX DLL. The...
5
by: kjworm | last post by:
Hello Everyone, I have been fighting with a type mismatch error for many hours today and I can't seem to find what the problem is. Hopefully it is more than a missing apostrophe! I have isolated...
19
by: Lysander | last post by:
I have written a query that takes three integers representing day,month and year, forms a date from them and compares this date to the date the record was entered and returns any records where the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.