Re: Problem with the output of Split
- From: "expvb" <nobody@xxxxxxx>
- Date: Sat, 17 Jan 2009 17:29:30 -0500
"cowznofsky" <jhcorey@xxxxxxxxx> wrote in message
news:9dd5823f-394c-402e-90cd-a3555a7a394e@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have comma-delimited text, and I'm trying to add the values to a
recordset in the most economical way possible. I've added two fields
to the recordset and made them both advarchar(30) just to test this.
The first AddNew statement works, but the second one throws an error
"Arguments are the wrong type, are out of acceptable range, or are in
conflict with each other".
The Array and Split statements here seem equivalent except when I try
to use them in this context.
Any idea what the difference is?
I was hoping to use Split on a long string rather than have to refer
to every field by an index.
Dim varFieldNames As Variant
varFieldNames = Array("itemDT", "Latitude")
myRS.Open
myRS.AddNew varFieldNames, Array("01-06-2009", "42.889901")
myRS.AddNew varFieldNames, Split("01-06-2009,42.889901", ",")
Array() returns an array of Variants, while Split returns an array of
Strings. This code illustrate this:
Option Explicit
Private Sub Form_Load()
Dim v As Variant
Dim i As Long
v = Array("01-06-2009", "42.889901")
Debug.Print LBound(v), UBound(v), TypeName(v)
For i = LBound(v) To UBound(v)
Debug.Print "'" & v(i) & "'"
Next
v = CVar(Split("01-06-2009,42.889901", ","))
Debug.Print LBound(v), UBound(v), TypeName(v)
For i = LBound(v) To UBound(v)
Debug.Print "'" & v(i) & "'"
Next
End Sub
It prints:
0 1 Variant()
'01-06-2009'
'42.889901'
0 1 String()
'01-06-2009'
'42.889901'
The solution is to use a Split alternative. Below are alternative Split
functions. They are slower than VB6 Split function when the number of
columns is below ~120. See "The Charts" heading in this page(First row under
VB6 shows VB's Split speed):
http://www.xbeat.net/vbspeed/c_Split.htm
Another solution is using ParseCSV01 function below. It seems twice as fast
as Split when the number of columns is small(~5):
http://www.xbeat.net/vbspeed/c_ParseCSV.php
You need to change the second parameter to "asValues() As Variant". This
will slow things down, but you can compare the speed.
Another option is using "INSERT INTO" statements, which should be faster if
all fields are strings. Example:
sSQL = "INSERT INTO MyTable (itemDT, Latitude) VALUES ('01-06-2009',
'42.889901')"
If some fields are Date or numeric, then you have to specify them
differently, using #mm/dd/yy#(Always US Date for MS Access) or using
DateValue('') which uses Control Panel settings. For numeric fields you have
to remove the quotes. You can loop through the Fields collection and check
the Type property to determine how to specify the values.
Microsoft Access SQL Reference:
http://msdn.microsoft.com/en-us/library/bb245488.aspx
Transact-SQL Reference(MS SQL Server)
http://msdn.microsoft.com/en-us/library/aa299742(SQL.80).aspx
.
- References:
- Problem with the output of Split
- From: cowznofsky
- Problem with the output of Split
- Prev by Date: count elements in root, sub root sub-sub root treeview
- Next by Date: Re: Simple DLL in VB6
- Previous by thread: Problem with the output of Split
- Next by thread: Labels etc on top of real Controls
- Index(es):
Relevant Pages
|