473,385 Members | 1,752 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,385 software developers and data experts.

Data type error in SQL query (Excel VBA)

6
Hi,

I have a quite complex UserForm and using SQL queries I retrieve from and also save data to another workbook (unfortunately I cannot use Access db in this case).

There are three particular textboxes in the form with linked checkboxes. Normally, users enter numbers to the fields and everything saves fine to the external workbook. But when the checkbox is ticked, it changes linked textbox value to "Unknown" and I get an error when saving that says something like "Data type error" (I can't post the actual expression as I have excel in Czech). Anyway, I checked and the query seems to generate ok. To make it even more complicated, there is one of the fields that saves without error under same conditions.

Here goes the code I use to generate SQL query:
Expand|Select|Wrap|Line Numbers
  1.  Dim SQL As String
  2.     SQL = "INSERT INTO [Report$A2:AM50000] ("
  3.  
  4.     Dim i As Control
  5.     For Each i In Me.controls
  6.         If TypeName(i) = "TextBox" Or TypeName(i) = "ComboBox" Then ' Take only Textboxes & Combos into account
  7.             If i.Value <> e Then SQL = SQL & i.Name & "," ' Only select not empty fields
  8.         ' ElseIf TypeName(i) = "CheckBox" Then
  9.         End If
  10.  
  11.     Next i
  12.     SQL = Mid(SQL, 1, Len(SQL) - 1) & ") VALUES(" ' Remove last space & comma
  13.  
  14.     Dim j As Control
  15.     For Each j In Me.controls
  16.  
  17.          If TypeName(j) = "TextBox" Or TypeName(j) = "ComboBox" Then
  18.  
  19.             If j.Value <> e Then
  20.  
  21.                 Select Case IsNumeric(j.Value)
  22.                     Case False
  23.                             SQL = SQL & "'" & j.Value & "'" ' Add single quotes around strings
  24.                     Case True
  25.                             SQL = SQL & j.Value
  26.                 End Select
  27.                 SQL = SQL & ","
  28.             End If
  29.         ' ElseIf TypeName(i) = "CheckBox" Then
  30.         End If
  31.     Next j
  32.     SQL = Mid(SQL, 1, Len(SQL) - 1) & ")"   ' Remove last comma
I tried CStr(textbox.text) but did not work. Does anyone know what's wrong?
Jun 27 '15 #1
0 1285

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

Similar topics

0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
4
by: Mark | last post by:
Bad header on my first post. Sorry. When I import a file using the Import Spreadsheet Wizard and I get to the Field Options screen where it allows me to choose the fields and the Indexed...
2
by: bloukopkoggelmander | last post by:
Hi all I am getting the following error and it is driving me insane. I just cannot find a fix for it. : You tried to assign the NULL value to a variable that is not a Variant data type. Now...
16
by: The Frog | last post by:
Hi Everyone, I have a small problem that doesnt seem to make any sense. I am using Access 97, and have a query that selects data from a text field, converts it to type Lng. This seems to work...
0
by: PowerWill500 | last post by:
Hi guys, I'm using a simple gdal script to load topography data from a .DEM file to a grid. However, when I do this gdal converts the data into 16 bit unsigned integers, which is no good for...
1
by: amrit1123 | last post by:
I m working on ms access 2007. and i want to create a table which have a field of datatype attachment through query u .like this (create table tablename (name varchar);). so, how to create a table...
4
by: Russel0725 | last post by:
This is the table structure I'm trying to query: Storename varchar 50 Bussinessdate datetime 8 txnnumber text 16 subtotal float 8...
4
by: shalskedar | last post by:
I want to transfer the excel data to th Db ..thru the VBA code. In the DB there is a table called outer2 containing 1 of the columns as "Upvc" whose format is set to Long integer. When i try to...
6
by: Annabelle Lee | last post by:
HeyHey, I'v got stucked on this problems for days already. What I am trying to do is to change the data type of a sepcific colume within a table which I am importing to my Access database. My...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.