saving input in a csv file VB script - vbscript

Below script capture in a raw text file. But I want to save input in a csv file into cell. Kindly help
Dim employee_id, employee_name, system_running_status
employee_id = InputBox("Enter your Employee ID", "Employee ID")
employee_name = InputBox("Enter your Name", "Employee Name")
fission_running_status = InputBox("Did you Powered-On the system today (Yes/No) ?","Fission Server Status")
Set obj = CreateObject("Scripting.FileSystemObject")
Const ForWriting = 8
Set obj1 = obj.OpenTextFile("aap.txt", ForWriting)
obj1.WriteLine ("Employee: " & employee_name & "-" & employee_id & " turned on system on: ") & Now() & vbCr
obj1.Close
Set obj=Nothing

You can simply reformat your output string:
Set obj1 = obj.OpenTextFile("aap.csv", ForWriting)
obj1.WriteLine employee_name & "," & employee_id & "," & Now() & vbCr

Related

VBA "INSERT INTO" error - Importing data from Excel

I'm using a VBA code in excel to pick some data and insert it into an Access DB. I need this for several tables.
The problem is: For some of those tables, I get an error like: "INSERT INTO syntax error". But, if I get the string the code generates and uses for inserting, and use it in SQL mode form Access, the query works just fine. So that doesn't make any sense. Here is a piece of it:
For j = 6 To lastrow
SQLStr = "INSERT INTO TENSILE(REFERENCE, REF_ID, POSITION, RATIO, YSL0, YSL90, YSL180, YSL270, YST0, YST90, YST180, YST270, UTSL0, UTSL90, UTSL180, UTSL270, UTST0, UTST90, UTST180, UTST270, EL0, EL90, EL180, EL270, ET0, ET90, ET180, ET270, ARL0, ARL90, ARL180, ARL270, ART0, ART90, ART180, ART270) SELECT '" & ws3.Cells(j, 1) & "', REF.ID,'" & ws3.Cells(j, 60).Value & "'"
For i = 61 To 93
SQLStr = SQLStr & "," & ws3.Cells(j, i).Value
Next i
SQLStr = SQLStr & " FROM REF WHERE REF.REFERENCE LIKE '" & ws3.Cells(j, 1) & "'"
ws3.Cells(7, 3).Value = SQLStr
MsgBox (SQLStr)
'rs.Open SQLStr, con, adOpenStatic, adLockOptimistic 'Opening the query
Next j
it's important to notice that this same structure is used for other tables and works normaly, like in:
For j = 6 To lastrow
SQLStr = "INSERT INTO METALOGRAPHY(REFERENCE, REF_ID, AUSTGRAINSIZE) SELECT '" & ws3.Cells(j, 1) & "',REF.ID ," & ws3.Cells(j, 18).Value & " FROM REF WHERE REF.REFERENCE LIKE '" & ws3.Cells(j, 1) & "'"
'MsgBox (SQLStr)
'ws3.Cells(2, 3).Value = SQLStr
rs.Open SQLStr, con, adOpenStatic, adLockOptimistic 'Opening the query
Next j
What is going wrong?
Have you considered changing how you are linked to the Access DB so that you could use a statement like:
db.execute SQLStr
This should solve your problem

VBA to Oracle PLSQL returns no results

I have a macro in excel 2013 that is trying to connect to an Oracle database through "Microsoft ODBC for Oracle", the 32bit version, as the 64bit has issues for some reason. And though the query works from Toad, it returns no results in my macro.
I have tried shorter queries and they do return results making me think that this monster query cannot be appended correctly, but I have no way to test this.
My current situation is a giant and very ugly append to get this query into one long string to be executed by my ADOConnection.
SQLString = "SELECT LEVEL, "
SQLString = SQLString & "MSIL.SEGMENT1 ITEM, "
SQLString = SQLString & "MSIL.DESCRIPTION DESC_COMPONENT, "
SQLString = SQLString & "BIC.ITEM_TYPE, "
SQLString = SQLString & "BIC.SUPPLY_TYPE, "
SQLString = SQLString & "MSIL.PRIMARY_UOM_CODE UOM, "
SQLString = SQLString & "BIC.COMPONENT_QUANTITY, "
SQLString = SQLString & "item_attribute.ITEM_LENGTH, "
SQLString = SQLString & "item_attribute.ANGLE_LEFT, "
SQLString = SQLString & "item_attribute.ANGLE_RIGHT, "
SQLString = SQLString & "item_attribute.WT_HT_FLAG, "
SQLString = SQLString & "item_attribute.FABRICATION_NUMBER, "
SQLString = SQLString & "item_attribute.PANEL_WIDTH, "
SQLString = SQLString & "item_attribute.PANEL_HEIGHT, item_attribute.GLASS_DIM1, item_attribute.GLASS_DIM2, item_attribute.DIMENSION1, item_attribute.DIMENSION2, "
SQLString = SQLString & "item_attribute.DIMENSION3, item_attribute.DIMENSION4, item_Attribute.DIMENSION5 "
SQLString = SQLString & "FROM APPS.MTL_SYSTEM_ITEMS MSI, APPS.BOM_BILL_OF_MATERIALS BOM, APPS.BOM_INVENTORY_COMPONENTS_V BIC, APPS.MTL_SYSTEM_ITEMS MSIL, APPS.mtl_parameters mp, "
SQLString = SQLString & "APPS.MTL_ITEM_CATALOG_GROUPS_VL micg, (SELECT * FROM (SELECT msi.inventory_item_id, mde.element_name, "
SQLString = SQLString & "mdev.element_value "
SQLString = SQLString & "FROM APPS.mtl_parameters mp, APPS.mtl_system_items_b msi, APPS.MTL_ITEM_CATALOG_GROUPS_VL micg, APPS.MTL_DESCRIPTIVE_ELEMENTS mde, APPS.MTL_DESCR_ELEMENT_VALUES mdev "
SQLString = SQLString & "WHERE mp.organization_id = MSI.organization_id AND mp.organization_code = 'POR' "
SQLString = SQLString & "AND micg.ITEM_CATALOG_GROUP_ID = MSI.ITEM_CATALOG_GROUP_ID AND mde.ITEM_CATALOG_GROUP_ID = micg.ITEM_CATALOG_GROUP_ID "
SQLString = SQLString & "AND mdev.element_name = mde.element_name AND mdev.inventory_item_id = msi.inventory_item_id AND mde.element_name IN ('Length', "
SQLString = SQLString & "'Angle Left', 'Angle Right', 'Wt Ht Flag', 'Fabrication Number', 'Panel Width', 'Panel Height', 'Glass Dim 1', 'Glass Dim 2', "
SQLString = SQLString & "'Dimension 1', 'Dimension 2', 'Dimension 3', 'Dimension 4', 'Dimension 5')) PIVOT (MAX (Element_value) FOR (element_name) "
SQLString = SQLString & "IN ('Length' AS ITEM_LENGTH, 'Angle Left' AS ANGLE_LEFT, 'Angle Right' AS ANGLE_RIGHT, 'Wt Ht Flag' AS WT_HT_FLAG, "
SQLString = SQLString & "'Fabrication Number' AS FABRICATION_NUMBER, 'Panel Width' AS PANEL_WIDTH, 'Panel Height' AS PANEL_HEIGHT, 'Glass Dim 1' AS GLASS_DIM1, "
SQLString = SQLString & "'Glass Dim 2' AS GLASS_DIM2, 'Dimension 1' AS DIMENSION1, 'Dimension 2' AS DIMENSION2, 'Dimension 3' AS DIMENSION3, "
SQLString = SQLString & "'Dimension 4' AS DIMENSION4, 'Dimension 5' AS DIMENSION5)) ORDER BY inventory_item_id) Item_attribute WHERE 1 = 1 "
SQLString = SQLString & "AND item_Attribute.inventory_item_id(+) = msil.INVENTORY_ITEM_ID AND BOM.ASSEMBLY_ITEM_ID = MSI.INVENTORY_ITEM_ID "
SQLString = SQLString & "AND BOM.ORGANIZATION_ID = MSI.ORGANIZATION_id AND bom.bill_sequence_id = bic.bill_sequence_id AND NVL (bic.disable_date, SYSDATE) >= SYSDATE "
SQLString = SQLString & "AND BIC.component_ITEM_ID = MSIL.INVENTORY_ITEM_ID AND Bom.ORGANIZATION_ID = MSIL.ORGANIZATION_ID AND mp.organization_id = msi.organization_id "
SQLString = SQLString & "AND mp.organization_code = 'POR' "
SQLString = SQLString & "AND micg.ITEM_CATALOG_GROUP_ID = msi.ITEM_CATALOG_GROUP_ID AND MSI.ORGANIZATION_ID = (SELECT DISTINCT OOL.SHIP_FROM_ORG_ID "
'These two lines are the ones that have our variables
SQLString = SQLString & "FROM APPS.OE_ORDER_HEADERS_ALL OOH, APPS.OE_ORDER_LINES_ALL OOL, APPS.MTL_SYSTEM_ITEMS MSI1 WHERE OOH.ORDER_NUMBER = '" & OracleOrderNumber & "' "
SQLString = SQLString & "AND OOL.LINE_NUMBER = '" & OracleLineNumber & "' "
'----------------------------------------------------
SQLString = SQLString & "AND OOL.HEADER_ID = OOH.HEADER_ID AND MSI1.SEGMENT1 = OOL.ORDERED_ITEM AND MSI1.ORGANIZATION_ID = OOL.SHIP_FROM_ORG_ID) "
SQLString = SQLString & "CONNECT BY PRIOR BIC.COMPONENT_ITEM_ID = BOM.ASSEMBLY_ITEM_ID START WITH MSI.INVENTORY_ITEM_ID IN (SELECT MSI1.INVENTORY_ITEM_ID "
SQLString = SQLString & "FROM APPS.OE_ORDER_HEADERS_ALL OOH, APPS.OE_ORDER_LINES_ALL OOL, APPS.MTL_SYSTEM_ITEMS MSI1 "
'These two lines are the ones that have our variables...again
SQLString = SQLString & "WHERE OOH.ORDER_NUMBER = '" & OracleOrderNumber & "' "
SQLString = SQLString & "AND OOL.LINE_NUMBER = '" & OracleLineNumber & "' "
'----------------------------------------------------
SQLString = SQLString & "AND OOL.HEADER_ID = OOH.HEADER_ID AND MSI1.SEGMENT1 = OOL.ORDERED_ITEM AND MSI1.ORGANIZATION_ID = OOL.SHIP_FROM_ORG_ID)"
There original query in it's cleaned up form is here.
SELECT LEVEL,
MSIL.SEGMENT1 ITEM,
MSIL.DESCRIPTION DESC_COMPONENT,
BIC.ITEM_TYPE,
BIC.SUPPLY_TYPE,
MSIL.PRIMARY_UOM_CODE UOM,
BIC.COMPONENT_QUANTITY,
item_attribute.ITEM_LENGTH,
item_attribute.ANGLE_LEFT,
item_attribute.ANGLE_RIGHT,
item_attribute.WT_HT_FLAG,
item_attribute.FABRICATION_NUMBER,
item_attribute.PANEL_WIDTH,
item_attribute.PANEL_HEIGHT,
item_attribute.GLASS_DIM1,
item_attribute.GLASS_DIM2,
item_attribute.DIMENSION1,
item_attribute.DIMENSION2,
item_attribute.DIMENSION3,
item_attribute.DIMENSION4,
item_attribute.DIMENSION5
FROM APPS.MTL_SYSTEM_ITEMS MSI,
APPS.BOM_BILL_OF_MATERIALS BOM,
APPS.BOM_INVENTORY_COMPONENTS_V BIC,
APPS.MTL_SYSTEM_ITEMS MSIL,
APPS.mtl_parameters mp,
APPS.MTL_ITEM_CATALOG_GROUPS_VL micg,
( SELECT *
FROM (SELECT msi.inventory_item_id,
mde.element_name,
mdev.element_value
FROM APPS.mtl_parameters mp,
APPS.mtl_system_items_b msi,
APPS.MTL_ITEM_CATALOG_GROUPS_VL micg,
APPS.MTL_DESCRIPTIVE_ELEMENTS mde,
APPS.MTL_DESCR_ELEMENT_VALUES mdev
WHERE mp.organization_id = msi.organization_id
AND mp.organization_code = 'POR' -- Warehouse -- POR(Portland), ITM (Item Master), TAC (Tacoma), TEM (Temecula), CMP (Composites)
AND micg.ITEM_CATALOG_GROUP_ID =
msi.ITEM_CATALOG_GROUP_ID
AND mde.ITEM_CATALOG_GROUP_ID =
micg.ITEM_CATALOG_GROUP_ID
AND mdev.element_name = mde.element_name
AND mdev.inventory_item_id = msi.inventory_item_id
AND mde.element_name IN ('Length',
'Angle Left',
'Angle Right',
'Wt Ht Flag',
'Fabrication Number',
'Panel Width',
'Panel Height',
'Glass Dim 1',
'Glass Dim 2',
'Dimension 1',
'Dimension 2',
'Dimension 3',
'Dimension 4',
'Dimension 5'))
PIVOT
(MAX (Element_value)
FOR (element_name)
IN ('Length' AS ITEM_LENGTH,
'Angle Left' AS ANGLE_LEFT,
'Angle Right' AS ANGLE_RIGHT,
'Wt Ht Flag' AS WT_HT_FLAG,
'Fabrication Number' AS FABRICATION_NUMBER,
'Panel Width' AS PANEL_WIDTH,
'Panel Height' AS PANEL_HEIGHT,
'Glass Dim 1' AS GLASS_DIM1,
'Glass Dim 2' AS GLASS_DIM2,
'Dimension 1' AS DIMENSION1,
'Dimension 2' AS DIMENSION2,
'Dimension 3' AS DIMENSION3,
'Dimension 4' AS DIMENSION4,
'Dimension 5' AS DIMENSION5))
ORDER BY inventory_item_id) Item_attribute
WHERE 1 = 1
AND item_Attribute.inventory_item_id(+) = msil.INVENTORY_ITEM_ID
AND BOM.ASSEMBLY_ITEM_ID = MSI.INVENTORY_ITEM_ID
AND BOM.ORGANIZATION_ID = MSI.ORGANIZATION_id
AND bom.bill_sequence_id = bic.bill_sequence_id
AND NVL (bic.disable_date, SYSDATE) >= SYSDATE
AND BIC.component_ITEM_ID = MSIL.INVENTORY_ITEM_ID
AND Bom.ORGANIZATION_ID = MSIL.ORGANIZATION_ID
AND mp.organization_id = msi.organization_id
AND mp.organization_code = 'POR' -- Warehouse -- POR(Portland), ITM (Item Master), TAC (Tacoma), TEM (Temecula), CMP (Composites)
AND micg.ITEM_CATALOG_GROUP_ID = msi.ITEM_CATALOG_GROUP_ID
AND MSI.ORGANIZATION_ID =
(SELECT DISTINCT OOL.SHIP_FROM_ORG_ID
FROM APPS.OE_ORDER_HEADERS_ALL OOH,
APPS.OE_ORDER_LINES_ALL OOL,
APPS.MTL_SYSTEM_ITEMS MSI1
WHERE OOH.ORDER_NUMBER = '1483' --Oracle Sales Order Number
AND OOL.LINE_NUMBER = '1' -- Oracle Sales Order Line Number
AND OOL.HEADER_ID = OOH.HEADER_ID
AND MSI1.SEGMENT1 = OOL.ORDERED_ITEM
--AND OOL.ORDERED_ITEM LIKE '%*%'
AND MSI1.ORGANIZATION_ID = OOL.SHIP_FROM_ORG_ID)
CONNECT BY PRIOR BIC.COMPONENT_ITEM_ID = BOM.ASSEMBLY_ITEM_ID
START WITH MSI.INVENTORY_ITEM_ID IN
(SELECT MSI1.INVENTORY_ITEM_ID
FROM APPS.OE_ORDER_HEADERS_ALL OOH,
APPS.OE_ORDER_LINES_ALL OOL,
APPS.MTL_SYSTEM_ITEMS MSI1
WHERE OOH.ORDER_NUMBER = '1483' --Oracle Sales Order Number
AND OOL.LINE_NUMBER = '1' -- Oracle Sales Order Line Number
AND OOL.HEADER_ID = OOH.HEADER_ID
AND MSI1.SEGMENT1 = OOL.ORDERED_ITEM
--AND OOL.ORDERED_ITEM like '%*%'
AND MSI1.ORGANIZATION_ID = OOL.SHIP_FROM_ORG_ID)
And thoughts would be fantastic, I have tired multiple drivers and installs of Oracle, connection does not seem to be my issue, it is how the query is executed.
As asked for here is my connection code...
Private Const ConnectionString = "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=string; uid=APPSRO; pwd=APPSRO;"
' ADO Connection Global Variable - Declared here so that, once the connection is opened, it can be used by any part of the code'
Public ADOConn As Object
Private OracleOrder As ADODB.Recordset
Private Const AppName = "Oracle BOM Data"
Private Const Version = "v1.0.0"
Public Const BoxTitle = AppName & " " & Version
Public Function ADOOpen(ByRef ADOConn As Object) As Boolean
' Open the ADO connection to the database and link it to the Object we've been passed'
' Handle errors here'
On Error Resume Next
' Clear the Error table'
Err.Clear
' Create a new connection and link it to the Object passed in'
Set ADOConn = CreateObject("ADODB.Connection")
' Now open the ADO connection to the database'
ADOConn.Open ConnectionString
'ADOConn.Properties("PLSQLRSet") = True'
' Now check for errors. Err.Number will be zero if there's been no error' since we cleared the Error table
If Err.Number = 0 Then
' So if there's been no error, return true'
ADOOpen = True
Else
' Otherwise, return false'
ADOOpen = False
End If
End Function
Ok, here is the actual function using the connections we know work...
Public Function LoadOracleOrder(OracleOrderNumber As Double, OracleLineNumber As Double)
Dim SQLString As String
Dim TargetSheet As Worksheet
Dim TargetCell As Range
Dim SQLQuery As String
On Error Resume Next
SQLString = "MONSTROUS QUERY HERE"
Debug.Print SQLString
Set OracleOrder = ADOFetch(SQLQuery, ADOConn)
If Err.Number <> 0 Then
WarnMsgBox "Warning: Query failed." & Chr(10) & "Error No: " & Err.Number & Chr(10) & "Error Description: " & Err.Description, BoxTitle
Err.Clear
ElseIf OracleOrder.RecordCount = 0 Then
InfoMsgBox "Query Returned No Results.", BoxTitle
Else
Set TargetSheet = Nothing
Set TargetSheet = ThisWorkbook.Sheets("Oracle Order Data")
If TargetSheet Is Nothing Then
Set TargetSheet = ThisWorkbook.Sheets.Add
TargetSheet.Name = "Oracle Order Data"
End If
TargetSheet.Cells.ClearContents
TargetSheet.Range("A1") = "LEVEL"
TargetSheet.Range("B1") = "ITEM"
TargetSheet.Range("C1") = "DESC_COMPONENT"
TargetSheet.Range("D1") = "ITEM_TYPE"
TargetSheet.Range("E1") = "SUPPLY_TYPE"
TargetSheet.Range("F1") = "UOM"
TargetSheet.Range("G1") = "COMPONENT_QUANTITY"
TargetSheet.Range("H1") = "ITEM_LENGTH"
TargetSheet.Range("I1") = "ANGLE_LEFT"
TargetSheet.Range("J1") = "ANGLE_RIGHT"
TargetSheet.Range("K1") = "WT_HT_FLAG"
TargetSheet.Range("L1") = "FABRICATION_NUMBER"
TargetSheet.Range("M1") = "GLASS_DIM1"
TargetSheet.Range("N1") = "GLASS_DIM2"
TargetSheet.Range("O1") = "DIMENSION1"
TargetSheet.Range("P1") = "DIMENSION2"
TargetSheet.Range("Q1") = "DIMENSION3"
TargetSheet.Range("R1") = "DIMENSION4"
TargetSheet.Range("S1") = "DIMENSION5"
Set TargetCell = TargetSheet.Range("A2")
OracleOrder.MoveFirst
While Not OracleOrder.EOF
TargetCell.Value = OracleOrder.Fields(0).Value
TargetCell.Offset(0, 1).Value = OracleOrder.Fields(1).Value
TargetCell.Offset(0, 2).Value = OracleOrder.Fields(2).Value
TargetCell.Offset(0, 3).Value = OracleOrder.Fields(3).Value
TargetCell.Offset(0, 4).Value = OracleOrder.Fields(4).Value
TargetCell.Offset(0, 5).Value = OracleOrder.Fields(5).Value
TargetCell.Offset(0, 6).Value = OracleOrder.Fields(6).Value
TargetCell.Offset(0, 7).Value = OracleOrder.Fields(7).Value
TargetCell.Offset(0, 8).Value = OracleOrder.Fields(8).Value
TargetCell.Offset(0, 9).Value = OracleOrder.Fields(9).Value
TargetCell.Offset(0, 10).Value = OracleOrder.Fields(10).Value
TargetCell.Offset(0, 11).Value = OracleOrder.Fields(11).Value
TargetCell.Offset(0, 12).Value = OracleOrder.Fields(12).Value
TargetCell.Offset(0, 13).Value = OracleOrder.Fields(13).Value
TargetCell.Offset(0, 14).Value = OracleOrder.Fields(14).Value
TargetCell.Offset(0, 15).Value = OracleOrder.Fields(15).Value
TargetCell.Offset(0, 16).Value = OracleOrder.Fields(16).Value
TargetCell.Offset(0, 17).Value = OracleOrder.Fields(17).Value
TargetCell.Offset(0, 18).Value = OracleOrder.Fields(18).Value
TargetCell.Offset(0, 19).Value = OracleOrder.Fields(19).Value
Set TargetCell = TargetCell.Offset(1, 0)
OracleOrder.MoveNext
Wend
End If
On Error GoTo 0
End Function
ADO Fetch, as asked...
Public Function ADOFetch(SQLQuery As String, ByRef ADOConn As Object) As Object
' Takes an existing, open ADO connection and uses it to return a Recordset, Nothing if no data is returned, or False if there's an error
' Local Object for the Recordset
Dim ADORecSet As Object
' Handle errors internally
On Error Resume Next
' Clear the Error table
Err.Clear
' Set up the Recordset by executing the SQL query
Set ADORecSet = ADOConn.ExecuteSQL(SQLQuery)
' Now check for errors. Err.Number will be zero if there's been no error since we cleared the Error table
If Err.Number = 0 Then
' So if there's been no error, return the record set
Set ADOFetch = ADORecSet
End If
' And either way, clear our temporary Recordset
ADORecSet = Nothing
On Error GoTo 0
End Function
While I understand you resolved your syntax issue, I thought I might suggest two approaches for best practices moving forward:
EXPLICIT JOIN
Use the current ANSI-syntax in joining tables in your query. Right now, your SQL is using the older, deprecated implicit join (keys being used in WHERE clause) versus the current ANSI-standard with the explicit join (keys used in INNER JOIN...ON). While this makes no difference in the query optimizer in terms of performance (see SO post), using INNER JOIN tends to be more readable and maintainable especially if you have various WHERE conditions to manage. Below is an attempt to translate your query with the INNER JOIN clause. Please test before use (notice WHERE is more readable):
SELECT LEVEL,
MSIL.SEGMENT1 ITEM,
MSIL.DESCRIPTION DESC_COMPONENT,
BIC.ITEM_TYPE,
BIC.SUPPLY_TYPE,
MSIL.PRIMARY_UOM_CODE UOM,
BIC.COMPONENT_QUANTITY,
item_attribute.ITEM_LENGTH,
item_attribute.ANGLE_LEFT,
item_attribute.ANGLE_RIGHT,
item_attribute.WT_HT_FLAG,
item_attribute.FABRICATION_NUMBER,
item_attribute.PANEL_WIDTH,
item_attribute.PANEL_HEIGHT,
item_attribute.GLASS_DIM1,
item_attribute.GLASS_DIM2,
item_attribute.DIMENSION1,
item_attribute.DIMENSION2,
item_attribute.DIMENSION3,
item_attribute.DIMENSION4,
item_attribute.DIMENSION5
FROM APPS.MTL_SYSTEM_ITEMS MSI
INNER JOIN APPS.BOM_BILL_OF_MATERIALS BOM
INNER JOIN APPS.BOM_INVENTORY_COMPONENTS_V BIC
INNER JOIN APPS.MTL_SYSTEM_ITEMS MSIL
INNER JOIN APPS.mtl_parameters mp
INNER JOIN APPS.MTL_ITEM_CATALOG_GROUPS_VL micg
INNER JOIN
( SELECT *
FROM (SELECT msi.inventory_item_id,
mde.element_name,
mdev.element_value
FROM APPS.mtl_parameters mp,
INNER JOIN APPS.mtl_system_items_b msi
INNER JOIN APPS.MTL_ITEM_CATALOG_GROUPS_VL micg
INNER JOIN APPS.MTL_DESCRIPTIVE_ELEMENTS mde
INNER JOIN APPS.MTL_DESCR_ELEMENT_VALUES mdev
ON mp.organization_id = msi.organization_id AND
ON micg.ITEM_CATALOG_GROUP_ID = msi.ITEM_CATALOG_GROUP_ID AND
ON mde.ITEM_CATALOG_GROUP_ID = micg.ITEM_CATALOG_GROUP_ID AND
ON mdev.element_name = mde.element_name AND
ON mdev.inventory_item_id = msi.inventory_item_id
WHERE mp.organization_code = 'POR' -- Warehouse -- POR(Portland), ITM (Item Master), TAC (Tacoma), TEM (Temecula), CMP (Composites)
AND mde.element_name IN ('Length',
'Angle Left',
'Angle Right',
'Wt Ht Flag',
'Fabrication Number',
'Panel Width',
'Panel Height',
'Glass Dim 1',
'Glass Dim 2',
'Dimension 1',
'Dimension 2',
'Dimension 3',
'Dimension 4',
'Dimension 5'))
PIVOT
(MAX (Element_value)
FOR (element_name)
IN ('Length' AS ITEM_LENGTH,
'Angle Left' AS ANGLE_LEFT,
'Angle Right' AS ANGLE_RIGHT,
'Wt Ht Flag' AS WT_HT_FLAG,
'Fabrication Number' AS FABRICATION_NUMBER,
'Panel Width' AS PANEL_WIDTH,
'Panel Height' AS PANEL_HEIGHT,
'Glass Dim 1' AS GLASS_DIM1,
'Glass Dim 2' AS GLASS_DIM2,
'Dimension 1' AS DIMENSION1,
'Dimension 2' AS DIMENSION2,
'Dimension 3' AS DIMENSION3,
'Dimension 4' AS DIMENSION4,
'Dimension 5' AS DIMENSION5))
ORDER BY inventory_item_id) Item_attribute
ON item_Attribute.inventory_item_id(+) = msil.INVENTORY_ITEM_ID AND
ON BOM.ASSEMBLY_ITEM_ID = MSI.INVENTORY_ITEM_ID AND
ON BOM.ORGANIZATION_ID = MSI.ORGANIZATION_id AND
ON bom.bill_sequence_id = bic.bill_sequence_id AND
ON BIC.component_ITEM_ID = MSIL.INVENTORY_ITEM_ID AND
ON Bom.ORGANIZATION_ID = MSIL.ORGANIZATION_ID AND
ON mp.organization_id = msi.organization_id AND
ON micg.ITEM_CATALOG_GROUP_ID = msi.ITEM_CATALOG_GROUP_ID
WHERE 1 = 1
AND NVL (bic.disable_date, SYSDATE) >= SYSDATE
AND mp.organization_code = 'POR' -- Warehouse -- POR(Portland), ITM (Item Master), TAC (Tacoma), TEM (Temecula), CMP (Composites)
AND MSI.ORGANIZATION_ID =
(SELECT DISTINCT OOL.SHIP_FROM_ORG_ID
FROM APPS.OE_ORDER_HEADERS_ALL OOH
INNER JOIN APPS.OE_ORDER_LINES_ALL OOL
INNER JOIN APPS.MTL_SYSTEM_ITEMS MSI1
ON OOL.HEADER_ID = OOH.HEADER_ID AND
ON MSI1.SEGMENT1 = OOL.ORDERED_ITEM AND
ON MSI1.ORGANIZATION_ID = OOL.SHIP_FROM_ORG_ID
WHERE OOH.ORDER_NUMBER = '1483' --Oracle Sales Order Number
AND OOL.LINE_NUMBER = '1' -- Oracle Sales Order Line Number
--AND OOL.ORDERED_ITEM LIKE '%*%'
)
CONNECT BY PRIOR BIC.COMPONENT_ITEM_ID = BOM.ASSEMBLY_ITEM_ID
START WITH MSI.INVENTORY_ITEM_ID IN
(SELECT MSI1.INVENTORY_ITEM_ID
FROM APPS.OE_ORDER_HEADERS_ALL OOH
INNER JOIN APPS.OE_ORDER_LINES_ALL OOL
INNER JOIN APPS.MTL_SYSTEM_ITEMS MSI1
ON OOL.HEADER_ID = OOH.HEADER_ID AND
ON MSI1.SEGMENT1 = OOL.ORDERED_ITEM AND
ON MSI1.ORGANIZATION_ID = OOL.SHIP_FROM_ORG_ID
WHERE OOH.ORDER_NUMBER = '1483' --Oracle Sales Order Number
AND OOL.LINE_NUMBER = '1' -- Oracle Sales Order Line Number
--AND OOL.ORDERED_ITEM like '%*%'
)
EXTERNAL .SQL FILE
Because VBA does not allow strings across multiple lines without underscores and ampersands, consider avoiding the need for a long concatenation of the "giant" VBA string. Instead, read in an external .sql file into the VBA string.
This will allow effortless change of the SQL query as you only need to copy/paste from your database console into the .sql text file and your Excel macro will read in the latest version without worrying about adjusting commas, underscores, and ampersands. Save below script in a separate function and in your query function, LoadOracleOrder(), simply call it as: SQLString = ReadSQL():
Function ReadSQL() As String
On Error GoTo ErrHandle
Dim objFSO As Object, ofile As Object
Dim sqlFile As String, strSQL As String, dataLine As String
Dim readFile As Integer, i As Integer
Const ForReading = 1
' INITIALIZE OBJECTS
sqlFile = ActiveWorkbook.Path & "\OracleQuery.sql"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ofile = objFSO.OpenTextFile(sqlFile, ForReading)
' OPEN FILE, READ LINES, CLOSE FILE
readFile = FreeFile()
Open sqlFile For Input As #readFile
strSQL = ""
Do While ofile.AtEndofStream <> True
strSQL = strSQL & ofile.ReadLine & vbNewLine
Loop
ReadSQL = strSQL
Close #readFile
ofile.Close
Set ofile = Nothing
Set objFSO = Nothing
Exit Function
ErrHandle:
MsgBox Err.Number & " - " & Err.Description, vbCritical
Set ofile = Nothing
Set objFSO = Nothing
Exit Function
End Function
Ok after running through issues that were overlooked due to my On Error Resume Next(thanks Tim), I found that I had a type mismatch, and I had a comma that should not have been in the sql (it had originally commented out but I must have changed it when appending the giant string).
Lastly, turns out there was a call to create a worksheet and then place the data in the project in which the macro was in (which is my personal file, not helpful.)
Thank you for your comments, and suggestions all of them helped me find the solution.

Fetching Column with table name alias is not working in VBA

I am trying to connect to Oracle table through VBA and fetching data. My situation is to reference one table multiple times to do pivot. But while running query via VBA it's throwing an error as I am fetching data through table alias: Below is a dummy code.
Sub Button1_Click()
Dim strDataSource, strUsername, strPassword, strConnectionString, strquery As String
Dim objADODBConnection, objADODBRecordset As Object
strDataSource = "xxxx"
strUsername = "xxxx"
strPassword = "xxxx"
strConnectionString = "Provider=MSDAORA;Data Source=" & strDataSource & ";Persist Security Info=True;Password=" & strPassword & ";User ID=" & strUsername
Set objADODBConnection = CreateObject("ADODB.Connection")
objADODBConnection.Open strConnectionString
Set objADODBRecordset = CreateObject("ADODB.Recordset")
strquery = "SELECT T1.Column1 FROM Table1 AS T1 WHERE Column2='XXXX' AND Column3='XXXX'"
Set objADODBRecordset = objADODBConnection.Execute(strquery)
MsgBox objADODBRecordset.Fields(0).Value
objADODBRecordset.Close
objADODBConnection.Close
Set objADODBRecordset = Nothing
Set objADODBConnection = Nothing
End Sub
The same query is running fine without table alias. Please suggest!!
FROM Table1 AS T1
You should remove 'AS' in from condition -
FROM Table1 T1

Use variable to enter into table values

I am unable to use a variable as the vale for a table, though direct values are stored.
I get the error as "Syntax error in INSERT INTO Statement.How do I overcome this ?
sDBPAth = App.Path & "\SETMDBPATH.mdb"
sConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDBPAth & ";" & _
"Jet OLEDB:Engine Type=4;"
' Type=4 to create an Access97 mdb, If omitted Access2000 mdb
' ------------------------
' Create New ADOX Object
' ------------------------
Set oDB = New ADOX.Catalog
oDB.Create sConStr
Set oCn = New ADODB.Connection
oCn.ConnectionString = sConStr
oCn.Open
Set oCM = New ADODB.Command
oCM.ActiveConnection = oCn
oCM.CommandText = "CREATE TABLE MDBPATH(" & _
"MDBPATH TEXT(40) NOT NULL," & _
"pass TEXT(10))"
oCM.Execute
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES (sDBPAth, 1,)" 'If 'sDBPath' is used the word sDBPath is stored not the variable value
'
' ------------------------
' Release / Destroy Objects
' ------------------------
If Not oCM Is Nothing Then Set oCM = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing
If Not oDB Is Nothing Then Set oDB = Nothing
End Sub
Try
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
You had an extra comma at the end, and you need to pass the variable in outside of the string.
You're relatively save in this case since you're not pass in a user input, but if you continue to build INSERT statements like above, you'll be vulnerable to SQL Injection attacks.
You need to amend your SQL statement to allow the application to substitute the sDBPath value
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
You need to call the variable outside of the string.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
If sDBPAth is an actual VB variable, you need to use something like:
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', '1')"
so that the insert statement is constructed from the value of the variable rather than a fixed string.
In addition, you appear to have a superfluous comma in your statement and you may need quotes around both values since they're both text type.

When I select the value from the combo box, related value should appear in the text box

When I select the value from the combo box, related value should appear in the text box
ComboBox code.
cmd.CommandText = "select distinct PERSONID from T_PERSON"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("PersonID")) Then
txtno.AddItem rs("PersonID")
End If
rs.MoveNext
Wend
In comboBox list of ID is displaying, when I select the particular person id, Name should display in text box related to the personid
Text Box
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("Name")) Then
txtName.Text = rs("Name")
rs.MoveNext
End If
Wend
I put the above code in Form_Load Event, Nothing displaying in Text Box.
What wrong in my code.
Need VB6 code Help
You would want the 2nd block of code in the the click event for the combobox.
Edit
There looks like another couple of issues in your code at this line:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
2 Issues:
You are passing in the control itself as the person ID, not the selected value.
You have an extra space in your query after the person ID
You should change that line to be:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
Why not have the combobox display the name and hold the personID as it's item data?
cmd.CommandText = "select distinct PERSONID, Name from T_PERSON WHERE PersonID IS NOT NULL"
Set rs = cmd.Execute
While Not rs.EOF
combo.AddItem rs("Name").value
combo.ItemData(combo.NewIndex) = rs("PERSONID").value
rs.MoveNext
Wend
Then, if you need the PersonID for the selected name you can just grab combo.ItemData(combo.ListIndex).

Resources