Recently at the company where I work I've needed to import a bunch of orders from excel into our database so that they can be processed and shipped. Many of these excel documents have hundreds of columns. I'm looking for a clean way to convert multiple rows and columns containing order data into a dump of SQL queries.
Here is what I'm working with.
Each order is a single row identified by the OrderID, and the quantity of each product is defined in the cell which corresponds to the ProductID.
I'm using the following formula to convert each cell (if filled) into a value for an SQL query:
=CONCATENATE(IF(ISBLANK(B3),"",("("&B3&", "&B$1&", "&$A3&"), ")))
The result for each filled cell is something like:
(1, 22, 1),
What I want to be able to do, is concatenate each cell (if filled) into one long string per order without having hundreds of columns filled with the same formula. Like this:
(5, 12, 23), (5, 13, 23), (100, 7, 23), (50, 1, 23), (100, 3, 23),
Thanks.
Try This -
VBA Code
Function InsStr(Product As Range, Order As String, Qty As Range)
Dim tmpString As String: tmpString = ""
Dim tmpCount As Integer: tmpCount = 1
For Each cell In Qty
If cell <> "" Then
If tmpCount > 1 Then tmpString = tmpString & ","
tmpString = tmpString & "(" & cell & "," & Product(tmpCount) & "," & Order & ")"
End If
tmpCount = tmpCount + 1
Next cell
InsStr = tmpString
End Function
and use Formula as =InsStr(B1:X1,A3,B3:X3)
Related
Trying to create a TextBox expression:
="Validity: " & IIF(Fields!ID.Value = 2, Fields!Value.Value, "") & " from date above."
from a dataset:
ID; NAME; VALUE;
1; Delivery; x Factory;
2; Validity; 30 days;
3; Pricing Structure; Subject to...;
so that the text box would read "Validity: 30 days from date above" but returns "Validity: from date above"
The problem is the report only allows me to use aggregate First, max, etc from the dataset producing an incorrect result.
"Validity: " & IIF(First(Fields!ID.Value, "DataSet") = 1, First(Fields!Value.Value, ), "") & " from date above."
"Validity: x Factory from date above"
Your dataset is showing "30 days", do you require the text box to show this or do you require it to be "60 days"?
Meanwhile if you restrict you dataset to one row of data, ie insert a where/having clause such as : HAVING (ID = 2), then you could use the aggregate sum function in your expression:
="Validity: " & IIF(Sum(Fields!ID.Value, "DataSet1") = 2, Fields!Value.Value, "") & " from date above."
I have an access 2013 table that houses one field with comma separated values. I have created a second table that I need to parse the results into with a structure like so
uPPID number
value1 short text
value2 short text
value3 short text
value4 short text
I am dynamically creating the table so it will always have enough "value" fields to accommodate for the number that will be parsed out. Sample data is like such:
uppID values
aeo031 boat, goat, hoat, moat
And I would want the field mappings to go like such
uPPID = aeo031
value1 = boat
value2 = goat
value3 = hoat
value4 = moat
How can access vba parse out a csv list from one field to many?
There are probably faster/better solutions than the follwing VBA loop that inserts records one by one in the destination table. But for instance it does the job.
TableCSV is the name of the source table
TableFields is the name of the destination table
The constant maxValues specifies the number of fields values available
The query composes dynamically the INSERT INTO statement after composing the values fields; it completes it to provide all the columns, and adds the surrounding quotes '...'. (p.s. it could be simplified if we can insert without specifying all column values..)
.
Sub splitTable()
Const maxValues As Long = 4 ' <-- Set to number of value fields in destination table
Dim query As String, values As String, rs
Set rs = CurrentDb.OpenRecordset("TableCSV")
Do Until rs.EOF
values = rs!values ' next we add commas to provide all fields
values = values & String(maxValues - UBound(Split(values, ",")) - 1, ",")
values = "'" & Replace(values, ",", "','") & "'" ' 'a', 'b', '', '' etc
query = "INSERT INTO TableFields VALUES (" & rs!uPPID & "," & values & ")"
Debug.Print query
CurrentDb.Execute query
rs.moveNext
Loop
End Sub
I have a field called Product_Id(type string), which has length of 7 and starting with 0. But while inserting through VBA into a table field of type text the zeros is not getting inserted.
This is the insert query:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES (" & prodID & ");"
I think I have fixed the error - you need to declare the value in single quotes.
The PROD_NBR is a string type and field in the table is text type, then the inserting variable should be declared inside single quotes then double quotes and between two & symbols:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES ('" & prodID & "');"
Responding to #Cherry's answer, the following method is less tedious than a parameterized query, aka prepared statement. prodID can safely contain quotes and other special characters.
With dbs.OpenRecordset("tablename")
.AddNew
.Fields("PROD_NBR") = prodID
.Update
End With
Regarding the "starting with zero" part of your question, do you want PROD_NBR to always be 7 characters, padded with leading 0's? Then replace prodID with:
Right("0000000" & prodID, 7)
This question already has answers here:
SSIS Transform -- Split one column into multiple columns
(4 answers)
Closed 9 years ago.
I have a Derived Column task set up in SSIS to split a FullName column into FirstName and LastName columns. The data comes in from a flat file (delimited text file, separated by commas) and looks like this:
|FullName|
|Lastname, Firstname Middlename|
Here is how I am trying to get the data to show up IMPORTANT NOTE - Not all records have a middle name:
|FirstName|LastName|
|Firstname M|Lastname|
The LastName column was easy to populate using Substring and Findstring appropriately. My issue now is pulling in the the first name and first letter of the middle name into the FirstName column. I wrote this:
SUBSTRING(Name, (FINDSTRING(FullName, ",", 1) + 1), (FINDSTRING(FullName, ",", 1) + 1))
You can use
SubString(
Right(FullName, Len(FullName) - FindString(FullName, ",", 1)),
1,
FindString(Right(FullName, Len(FullName) - FindString(FullName, ",", 1)), " ", 1) > 0
?
FindString(Right(FullName, Len(FullName) - FindString(FullName, ",", 1)), " ", 1) + 1
:
Len(Right(FullName, Len(FullName) - FindString(FullName, ",", 1)))
)
The Right(FullName, Len(FullName) - FindString(FullName, ",", 1)) part appearing in the code several times extracts Firstname Middlename. Then the Substring around it cuts off after the initial of the middle name.
And to take care of the case that there may be no space in this Firstname Middlename, there is a condition as the last argument of SubString which either returns the length of Firstname M, or the length of the whole string, in which case SubString returns the whole string.
I hope I got all positions correct, and not one off to the left or right.
Using Access 2003
Date Column datatype is text
Table1
Date
20090528
20090529
20090530
20090502
20090504
Expected Output
28-May-2009
29-May-2009
30-May-2009
02-May-2009
04-May-2009
...,
How to make a query for the Expected Output Date format?
As VBA code - you could wrap it as a function
Dim strMyDate As String
Dim dteDate As Date
strDate = "20090528"
dteDate = DateSerial(Left(strDate, 4), Mid(strDate, 5, 2), Right(strDate, 2))
MyStr = Format(dteDate, "dd-mmm-yyyy")
Debug.Print MyStr
As a Data type in a Table - If you append the data to a table where the field is Date/Time formatted then you can specify the format on the Form / Table i.e. at output time.
I find that Cdate does not work for me.
Format(DateSerial(Left(Field1, 4), Mid(Field1, 2, 2), Right(Field1, 2)), "dd-mmm-yyyy")
CDate should do what you want...