473,769 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type Mismatch Error

I am getting "Type Mismatch Error" when the following code executes. I
am trying to notify the user if she attempts to add a customer with the
same FirstName, LastName, Address(line1) and City as one that already
exists on the file.

If there is a match, ask the user and cancel the record add if she sees
that it's a dup customer.

I had trouble with the line continuations, quotes and continuation
chracters, and noticed that Access added a " on the top line for me.

There are no dimensions for any of these fields; this is the only place
they are used.
' Makes sure the user entered a new customer before saving

Private Sub cmdClose_Click( )
On Error GoTo Err_cmdClose_Cl ick

' to save new cust so they can add garments

Dim lngCustID As Long

' saves the new customer record in tblCustomers

' Makes sure the user entered a new customer before saving

If Me.Dirty = True Then
lngCustID = Me.txtCustID
DoCmd.RunComman d acCmdSaveRecord
' Requery the customer list to see the new customer as well
Forms!frmFindCu stomer.lstCusto mers.Requery
Forms!frmFindCu stomer.lstCusto mers = lngCustID
End If

DoCmd.Close

Exit_cmdClose_C lick:
Exit Sub

Err_cmdClose_Cl ick:

' If the user enters the same first name, last name, street1 and
city as one on file,
' check to see if this is truly a different customer (dup index)
' of if this was an error. Allow save if different; cancel if error

' If response is NOT Yes, Me.undo and get out (recognize it's a
dup-do NOT add)
' If response is YES, add record

If Err.Number = 3022 Then
If MsgBox("Custome r " & Me.txtFirstName & " " &
Me.txtLastName, "" _
& " " & Me.txtAddress1 & " " & Me.txtCity & " exists. "
_
& " Are you sure you want to add this one too?" _
& vbYesNoCancel + vbExclamation, "Duplicate Customer") <>
vbYes Then
Me.Undo
GoTo Exit_cmdClose_C lick
End If
End If
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdClose_C lick

End Sub

Many thanks.
Sara

Nov 13 '05
19 2087
Thanks. I totally understand and will work on this (along with other
coding hints) right now to "upgrade" my style.

I am so very happy to receive tips and help as there is no one I can
ask at work (I am the only "coder" in the company) and my user group
meets only once per month!

Sara

Nov 13 '05 #11
"sara" <sa*******@yaho o.com> wrote in
news:11******** **************@ g44g2000cwa.goo glegroups.com:
Thank you - it works!

I think my confustion is around when to use the & on a
continuation line and when not to. Is there anything on the
web on this? I have looked around in Help and the web and the
3 books I have and found nothing.

I have had to use the continuation character AND the & AND
quotes at the end of each line and the beginning of the next
when building a SQL string (like "Where" clause) in code. It
appears, however that the extra ampersand and quotes are only
needed when doing continuations of text that should be in
quotes - or something like that?

The comma was my error.
Thanks
Sara

The continuation flag _ must come at every line in a statement
except the last one. It must be outside a string.

The concatenation operator & must come between any two strings.

So if you have to break a string into two parts to use the
continuation flag, you need an & character.and this must be
followed by a quotation mark ", or by a string variable.

You don't use the concatenation operator if it precedes an
Access keyword.

Any SQL statement is always a string, and nothing but a string,
there you always need the concatenation operator.

You may find it a lot easier if you were to Dim and build a
string variable, and put that in the messagebox statement.
Dim stMsg as string

stMsg = "Customer " & Me.txtFirstName & " " & Me.txtLastName"
stMsg = stMsg & " " & Me.txtAddress1 & " " & Me.txtCity
stmsg = stmsg & " exists. "
stmsg = stMsg & " Are you sure you want to add this one too?"

If MsgBox( stMsg, _
vbYesNoCancel + vbExclamation, _
"Duplicate Customer") _
<> vbYes Then

You dont need a & before "Duplicate customer", because you
aren't breaking a string into parts.

Once you have the stMsg working as multiple statements you can
simply replace the stMsg = stMsg with a _ ot the end of the line
above.

HTH..

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #12
Bri


lylefair wrote:
John Mishefske wrote:

And to expand on David's response - you want to use Me.Dirty = False because it is very
explicit about what it is going to do. It will save a record on the current form (Me).

Using DoCmd.RunComman d acCmdSaveRecord will save a record on whatever form has the current
focus. That might not be the form you expect it to be.
Good luck on your project.

Could you explain this further, please? Sara's "DoCmd.RunComma nd
acCmdSaveRecord " appears in a Private Sub (Private Sub
cmdClose_Click( )) seeiminglt in a Form Module in this context:

If Me.Dirty = True Then
lngCustID = Me.txtCustID
DoCmd.RunComman d acCmdSaveRecord

In what situations will this command run when the form in which it
resides (the Me form) does not have the focus?

What is the code in an Object other than the Form1 module to call
Form1's cmdClose_Click( )?
What is similar code to do so while Form2 or other form has the focus?


If you are debugging this code, then the active window is the code
module not the form so you get the "acCmdSaveRecor d is not available
now" error. Of course, at runtime this isn't a problem.

--
Bri
Nov 13 '05 #13
OK. I'm getting this and maybe even going to get good at it. I have
tried to build a SQL string and run it in my code, to change the
customer status to "I" (Inactive) along with all the items that belong
to that customer, if my user says she wants to Delete a customer.
(She's never used a computer before, is nervous, and may delete in
error).

My query runs, so I tried to convert the query SQL into a string and
run it.

Query Code:
UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON tblCustomers.Cu stID =
tblCustomerItem s.CustID SET tblCustomers.Cu stStatus = "I",
tblCustomerItem s.ItemStatus = "I"
WHERE (((tblCustomers .CustID)=[Forms]![frmCust]![txtCustID]));

MyCode:
strWhere = " WHERE tblCustomers.Cu stID = " &
Forms!frmCust!t xtCustID
strInactive = "I"
strSQL = "UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON " _
& " tblCustomers.Cu stID = tblCustomerItem s.CustID " _
& "SET tblCustomers.Cu stStatus = " & strInactive & "," _
& " tblCustomerItem s.ItemStatus = " & strInactive & strWhere
' Run the Update Query
db.Execute strSQL

I put in a breakpoint and stepped through the code (I'm learning that,
too) and ?strSQL to see the SQL:

UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON tblCustomers.Cu stID
= tblCustomerItem s.CustID SET tblCustomers.Cu stStatus = I,
tblCustomerItem s.ItemStatus = I WHERE tblCustomers.Cu stID = 15

This looks correct to me, as I compare it to the SQL view of the query.
I did see extra spaces here and there, but even removing them gives me
the error:

91 Object Variable or With Block Variable not set

I'm cluless, as I don't see any variables I've left Unset. I tried
making the strInactive = " 'I' " and that didn't work.
I tried saying strSQL = and put in the resolved statement, even though
my resolution seemed ok.
I tried adding a comma before "WHERE"

All give me the same error. Don't know what else to try.
thanks

Nov 13 '05 #14
"lylefair" <ly***********@ aim.com> wrote in
news:11******** **************@ f14g2000cwb.goo glegroups.com:
Could you explain this further, please? Sara's "DoCmd.RunComma nd
acCmdSaveRecord " appears in a Private Sub (Private Sub
cmdClose_Click( )) seeiminglt in a Form Module in this context:

If Me.Dirty = True Then
lngCustID = Me.txtCustID
DoCmd.RunComman d acCmdSaveRecord

In what situations will this command run when the form in which it
resides (the Me form) does not have the focus?


If, say, there is a time running in a background form and it brings
the form to the front sometime after the subroutine with
DoCmd.RunComman d acCmdSaveRecord was initiated that could easily
mean that the first two lines of the code snippet execute after the
other form has the focus, and the third line would then save the
record in the form that was brought to the foreground by your timer
event.

Of course, I avoid timers wherever possible, but some features
require them.

I don't consider the focus issue to be a strong reason to use
Me.Dirty = False to save a record -- it's just a side benefit, in my
opinion. I prefer it for logical reasons, as it is more clear what
it is doing, because the command shows that it's acting on a
particular object and changing a property of that object. That seems
to me to be a better way of coding than just running a menu command.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #15
"sara" <sa*******@yaho o.com> wrote in
news:11******** **************@ g44g2000cwa.goo glegroups.com:
I think my confustion is around when to use the & on a
continuation line and when not to.


Write the code first on a single line. Then break it with line
continuation characters. If you're breaking in the middle of a
string value, you have to surround the line continuation character
with quotes and ampersand.

So, where you insert a line break in the code, you have to add
(using braekets to serve as quote markes) [" _] at the end, and then
add [& "] at the beginning of the next line.

Consider, if you have this string:

"This is a string that I want to break into two lines."

You could change that to:

"This is a string that I" & " want to break into two lines."

That's pretty clearly equivalent. Then all you have to do is add the
line continuation character:

"This is a string that I" _ & " want to break into two lines."

then the line break:

"This is a string that I" _
& " want to break into two lines."

Does that make more sense?

However, in your original problem, the real issue was that your
string concatenation was all happening as an argument that was a
parameter of an Access command. In that kind of case, you end up
with more readable and more maintainable code if you break it all
down into multiple stagee, as someone suggested:

1. create a variable to store your string value.

2. assign the string to that variable

3. use that variable in the line executing the command.

In terms of the MsgBox function, that makes it much more readable.

I'd also suggest another little hint that I got from others here in
CDMA. Instead of

If MsgBox(...) = vbYes Then

change it to

If vbYes - MsgBox(...) Then

THis makes it much easier to see what you're testing for in your
If/Then statement.

Last of all, let me say that I generally don't use line continuation
characters in any of my code -- I just scroll over to the right. I
know that's something that drives many people absolutely nuts, but
it's exactly the kind of problems you're having that has led me to
avoid using them. I don't find the broken up lines any more readable
than the long ones that I have to scroll through.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #16
Comments in-line.
"sara" <sa*******@yaho o.com> wrote in
news:11******** ************@g4 4g2000cwa.googl egroups.com:
OK. I'm getting this and maybe even going to get good at it.
I have tried to build a SQL string and run it in my code, to
change the customer status to "I" (Inactive) along with all
the items that belong to that customer, if my user says she
wants to Delete a customer. (She's never used a computer
before, is nervous, and may delete in error).

My query runs, so I tried to convert the query SQL into a
string and run it.

Query Code:
UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON
tblCustomers.Cu stID = tblCustomerItem s.CustID SET
tblCustomers.Cu stStatus = "I", tblCustomerItem s.ItemStatus =
"I" WHERE
(((tblCustomers .CustID)=[Forms]![frmCust]![txtCustID]));

MyCode:
strWhere = " WHERE tblCustomers.Cu stID = " &
Forms!frmCust!t xtCustID
strInactive = "I"
strSQL = "UPDATE tblCustomers LEFT JOIN tblCustomerItem s
ON " _
& " tblCustomers.Cu stID = tblCustomerItem s.CustID " _
& "SET tblCustomers.Cu stStatus = " & strInactive &
"," _ & " tblCustomerItem s.ItemStatus = " &
strInactive & strWhere
' Run the Update Query
db.Execute strSQL

I put in a breakpoint and stepped through the code (I'm
learning that, too) and ?strSQL to see the SQL:

UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON
tblCustomers.Cu stID = tblCustomerItem s.CustID SET
tblCustomers.Cu stStatus = I, tblCustomerItem s.ItemStatus = I
WHERE tblCustomers.Cu stID = 15

This looks correct to me, as I compare it to the SQL view of
the query.
I did see extra spaces here and there, but even removing them
gives me
the error:

91 Object Variable or With Block Variable not set
If you compare the two sql statements, you' will see a slight
difference.
tblCustomers.Cu stStatus = "I", vs
tblCustomers.Cu stStatus = I,

Notice that you need to put quotes around the value.

& "SET tblCustomers.Cu stStatus = " & strInactive & "," _
& " tblCustomerItem s.ItemStatus = " & strInactive & strWhere

needs to be
& "SET tblCustomers.Cu stStatus = """ & strInactive & """," _
& " tblCustomerItem s.ItemStatus = """ & strInactive & """" _
& strWhere

To embed a quote in the string you have to double it up. You
might also need quotes in hte whereclause, if custID is defrined
as text in the table.
I'm cluless, as I don't see any variables I've left Unset. I
tried making the strInactive = " 'I' " and that didn't work.
I tried saying strSQL = and put in the resolved statement,
even though my resolution seemed ok.
I tried adding a comma before "WHERE"
Finally, Access likes to see a semicolon at the end of a sql
statement. You need to add that too. The last line would become
& strWhere & ";"

All give me the same error. Don't know what else to try.
thanks

HTH
--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #17
I'm now getting "91: Object variable or With Block Variable not set".
The code goes to my error routine right after the
"db.execute strSQL" statement.

I've been working on this since last night and most of today. My query
statement (resolved - strSQL) is identical to the query SQL (that works
when I call the query from the code), thanks to all the help on coding
such strings.

UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON tblCustomers.Cu stID
= tblCustomerItem s.CustID SET tblCustomers.Cu stStatus = "I",
tblCustomerItem s.ItemStatus = "I" WHERE tblCustomers.Cu stID = 15;

I tried to get quotes around the "15" as it is txtCustID, but I failed
on that. If that is the problem, it's back to coding strings!

Code:

Private Sub cmdDeleteCust_C lick()
On Error GoTo Err_cmdDeleteCu st_Click

Dim strSQL As String ' for Update Query
Dim strWhere As String ' for Update Query
Dim txtCustID As String
' to hold I for query - avoids need for quotes in string build
Dim strInactive As String
Dim db As DAO.Database ' Current database

If MsgBox _
("Are you sure? Pressing Yes will remove the customer" & _
" and all customer items", vbYesNoCancel + vbExclamation,
"Delete Customer") _
<> vbYes Then
Exit Sub
End If

' If the user wants to "delete", change the customer status to
Inactive,
' updqChangeItemS tatus - changes all item statuses for this customer
to I

' Turn off warnings (and then back on) so user doesn't get message
about updates
DoCmd.SetWarnin gs False

' DoCmd.OpenQuery ("updqChangeCus tAndItemStatus" )

txtCustID = Forms!frmCust!t xtCustID

strWhere = " WHERE tblCustomers.Cu stID = " & txtCustID
strInactive = "I"

strSQL = "UPDATE tblCustomers LEFT JOIN tblCustomerItem s ON " _
& " tblCustomers.Cu stID = tblCustomerItem s.CustID " _
& "SET tblCustomers.Cu stStatus = """ & strInactive & """," _
& " tblCustomerItem s.ItemStatus = """ & strInactive & """" &
strWhere & ";"

' Run the Update Query

db.Execute strSQL ' goes from here to error when stepping thru
code

DoCmd.SetWarnin gs True

' Set the criteria back to what the user entered and requery the form
' Deleted customer should be gone

DoCmd.Close acForm, Me.Name

Forms!frmFindCu stomer.lstCusto mers = Me.txtCustID
Forms!frmFindCu stomer.lstCusto mers.Requery

Exit_cmdDeleteC ust_Click:
Exit Sub

Err_cmdDeleteCu st_Click:
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdDeleteC ust_Click

End Sub

Hopefully you can help with this? I want to execute the query in the
code, both to learn how, and to hope it will trigger the before_update
event so I can capture the changes in an audit table.

Thank you very much.
sara

Nov 13 '05 #18
sara wrote:
I'm now getting "91: Object variable or With Block Variable not set".
The code goes to my error routine right after the
"db.execute strSQL" statement.


Guessing?

You dim'd the db object but you never instantiated it. Put something
like this in there before you try to use the db object:

Set db = Currentdb()

HTH
--
Smartin
Nov 13 '05 #19
Oh my! It worked! I am learning and so very appreciative of all the
help.

Until next problem...

Sara

Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10637
by: leslie_tighe | last post by:
Hello, I have a method on a com+ object that is returning an array of objects. I know the array is popluated as calls to check the ubound and lbound show valid values. However, any calls to get the value of a cell in the array results in a type mismatch error.
4
11967
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 14 (STDCOST). ---End Error Msg--- The STDCOST is set to decimal (28,14) and is a formatted in Access as a number, single with 14 decimal. I don't know why I would be getting a Type
6
3906
by: shan | last post by:
What is the meaning for the error expression syntax and type mismatch error.I am using turbo c++.can anybody correct the errors in the folowing program. Following program is to find matrix addition. Thanks in advance. #include<stdio.h> int rows,cols,a,b;
1
2045
by: jodyblau | last post by:
I am getting a type mismatch message under strange circumstances. Here's whats going on: 1. I have split the database into a front end and a back end. 2. I have compiled the project. 3. I update the linked tables and then make an mde file. 4. When I use the mdb file, everything seems to work fine.
2
1940
by: Rehan | last post by:
Hi there! Please help me out here. I am an inch away from completing my assignment. I am very new to VBA but i have very less time to be efficient at it. I am getting a type mismatch error when i click the button whose code is:
5
2849
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 each SQL statement using a MsgBox and then input that exact code as a Query in access which works. I'm using Access 97 on Windows XP. My code is below: Dim strKaizen As String strKaizen = "UPDATE KaizenEvents SET(TeamLeader)= '" & & "' WHERE...
7
5363
by: Mike | last post by:
Type Mismatch error I recieve a type mismatch error on the following line of code which is based on a specific date. It does NOT break on all records only "some". The dates for the records that ARE breaking are no different then the ones that are NOT breaking... Any hely is appreciated... Mike CODE:
5
9956
by: Lara1 | last post by:
Hi, I'm a total beginner to VBA, so please bear with me if I seem a bit dense. What I'm Trying to Achieve I'm trying to write a procedure in Excel, which is supposed to - look at the pH values stored in column E, row by row - compare this to a threshold value of 5 - enter a string ("pH range C") in column K, if the value in column E exceeds 5.
1
4048
by: sharbha | last post by:
I am experiencing a VBScript runtime error. The error message is: "(0x800A000D)Type mismatch error" Here is my code: <% dim temp temp=session("items") session("count")=session("count")+1 redim preserve items(session("count"))
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.