473,396 Members | 1,748 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.

Run-time error '13': Type mismatch

675 512MB
I have a form which is opened by with
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "fEditTitle", acNormal, , , , acDialog, "Key=" & txtKey
The Form Load for fEditTitle is
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Me.Filter = OpenArgs
  3.     Me.FilterOn = True
  4. End Sub
So far, so good. The form opens with all controls displayed correctly. When I attempt to change a textbox, the following fires
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtTitle_Change()
  2.     Call TidyForm(txtTitle.Text)
  3. End Sub
and the code for TidyForm is
Expand|Select|Wrap|Line Numbers
  1. Private Sub TidyForm(strTitle As String)
  2. Dim curValue As Currency
  3. Dim wkTitle As String
  4.     wkTitle = strTitle
  5.     curValue = Val(wk)
  6.     ...
  7. End Sub
In the immediate window, I then try
Expand|Select|Wrap|Line Numbers
  1. ?Val("12 xy")
and get the same error '13'. Much is locked because the form is modal. So I close the form, and the immediate window works, but the code, now stopped, will not get by the statement "curValue = Val(wk)". "CurValue = CInt(wk)" is OK if wk is numeric, so VBA can find the function libraries.
I've done a Compact and Repair, and a Decompile. I've done Debug->Compile and there are no errors. I changed the offending statement to assign into a Variant instead of Currency, but no good.
What is wrong?
Jul 25 '09 #1
9 13086
ADezii
8,834 Expert 8TB
@OldBirdman
Hello OldBirdman, where is the Declaration for wk, where is it Initialized, and what does it Represent?
Jul 25 '09 #2
OldBirdman
675 512MB
Error in moving relevant code to forum. Code should have read
Expand|Select|Wrap|Line Numbers
  1. Private Sub TidyForm(strTitle As String) 
  2. Dim curValue As Currency 
  3. Dim wkTitle As String 
  4.     wkTitle = strTitle 
  5.     curValue = Val(wkTitle) 
  6.     ... 
  7. End Sub 
I always have "Option Explicit" and run Debug->Compile, so it can't be that easy an error. I'm sorry about the typo, but if that were my error, I would have expected unknown variable and not error 13.
Jul 26 '09 #3
ADezii
8,834 Expert 8TB
@OldBirdman
The following code will work nicely, and will always ensure that the Value typed into [txtTitle] can always be converted to Currency. You will always get a Type Mismatch on any Non-Numeric Value entered into [txtTitle], except a (.), since you are attempting to Coerce it into a Currency data Type, namely curValue.
Expand|Select|Wrap|Line Numbers
  1. Private Sub TidyForm(strTitle As String)
  2. On Error GoTo Err_Tidyform
  3. Dim curValue As Currency
  4.  
  5. 'Will produce Type Mismatch Error (13) if the Vale contained in the Text
  6. 'Property cannot be converted to Currency
  7. curValue = CCur(strTitle)
  8.  
  9. Exit_Tidyform:
  10.   Exit Sub
  11.  
  12. Err_Tidyform:
  13.   Select Case Err.Number
  14.     Case 13     'Type Mismatch - remove entry since it cannot be
  15.       'converted to Currency
  16.       Me![txtTitle] = Null
  17.     Case Else
  18.       MsgBox Err.Description & Err.Number, vbExclamation, "Error in Tidyform()"
  19.     End Select
  20.       Resume Exit_Tidyform
  21. End Sub
Jul 26 '09 #4
OldBirdman
675 512MB
I don't want to know whether txtTitle is a number, I want the value of the leading digits, if any. This is so I can sort the titles using the value of the leading numbers instead of the text.

According to Access Help, Val() always returns a number (except if argument is null). If no leading digits, a zero is returned, which I test for and change to 9999999999, so the number sort first.

Even ignoring the currency, this fails. Assignment to a Long or Variant, I get error 13.
Jul 27 '09 #5
Lysander
344 Expert 100+
I have just run your code (in Access 2003) and it works fine if I supply strTitle with text, numeric or mixed.

Try putting a breakpoint at "wkTitle = strTitle" and see what value of strTitle is accutally getting to the Val(wkTitle) statement.
Jul 27 '09 #6
OldBirdman
675 512MB
wkTitle has the value "3 Way", the name of a movie. I know this because the code stops with error 13, and I can mouseover or ?wkTitle. Notice from my original post I tried the same function in the immediate window with the same result. ?Val("12 Dozen") gets error 13 in immediate window, but ?CInt("12.22") returns a 12, which is correct.
I suspect that the library reference is incorrect.
Typing this response, I realize that I do have a field in a table called Val. Is this my problem? It is never referenced within VBA as a variable. It does occur within query statements, within quotes, in many places in VBA. strSQL = "... ORDER BY Val, Title" for example.
Jul 27 '09 #7
OldBirdman
675 512MB
Forget it. This is the problem. I was using the name Val for a field in a table, and the error 13 / type mismatch was that Val() conflicts with rst.Val, even though Val doesn't appear outside quotes in the VBA code.

Thanks ADezii and Lysander for your time. I wouldn't have found it without your help. In typing my previous response, I started to become aware when I realized maybe a library reference was bad, but then other references were OK.
Jul 27 '09 #8
Lysander
344 Expert 100+
Checking for a field or function called Val was my next thought.

The most common occurence of this is having a field called "Name" which is another reservered word. It would be nice if Access could enforce the reservered words and stopped us using them as variables or fields.

Glad it is all fixed now.
Jul 27 '09 #9
OldBirdman
675 512MB
Yes, and it would also be nice if this possibility were documented for this error. A Google search to try to solve this always assumes a typo or other 'stupid' error, whereas avoiding keywords is a real hassle. There are too many to remember. In this case, I should have remembered, but variables in VBA can't be keywords, but I don't remember reading that table field names can't be VBA keywords. Doesn't matter until you start using VBA, then it bytes you in the ...
Jul 27 '09 #10

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

Similar topics

4
by: Ed | last post by:
Hello, I took a course in asp about 2 years ago and I was practicing with IIS 5.0. Then I put it down for a while. Now trying to get back to it. I can't run asp files from subdirectories of...
2
by: Jenna Olson | last post by:
Hi all- I've never seen this particular issue addressed, but was wondering if there's anything to support one way or another. Say I have a class: class ManipulateData { public:...
15
by: mg | last post by:
How can I run an .exe using C# from within the code behind of a WebForm app?
6
by: billr | last post by:
I have developed a small API for taking care of a lot of boiler plate stuff in a multi formed windows application, for example setting up a messaging thread framework. New Forms, in the...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
9
by: Brett Wesoloski | last post by:
I am new to VS2005. I changed my program.cs file to be a different form I am working on. But when I go to run the application it still brings up the form that was originally declared as new. ...
7
by: Lee Crabtree | last post by:
I remember when I was first getting into .NET Forms programming that there was a rather emphatic rule about not constructing a form before calling Application.Run with it. So this: ...
8
by: David Thielen | last post by:
Hi; In our setup program how do I determine if I need to run "aspnet_regiis –i" and if so, is there an API I can calll rather than finding that program on the user's disk and calling it? --...
3
by: traceable1 | last post by:
Is there a way I can set up a SQL script to run when the instance starts up? SQL Server 2005 SP2 thanks!
7
by: mxdevit | last post by:
Task: run application from ASP.NET for example, you have a button on ASP.NET page, when press this button - one application is invoked. the code to run application (for example, notepad) is...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.