I am having trouble querying a column in an Oracle view that shows up when I pull the schema. In fact, it appears as column number 2 when I list it out.
The error indicates ORA-00904 invalid identifier, which from what I have read says the column name I am referencing is incorrect, but I have copied the name directly from Oracle Developer, MSAccess, and the datareader.Schema, all of which appear to have no issues getting to that column.
If I query the column just using a linked table in MSAccess the data also comes right up. All of the examples I have seen referencing a similar issue in which the field is incorrectly typed, which though I acknowledge is still a possibility, seems unlikely in this case given the direct copy from the column list as mentioned.
Other solutions mention putting the name in double quotes, which I am uncertain how to do in VB.NET or if it is even necessary.
Code below:
'Open And Query
oledbCon.ConnectionString = strCon
oledbCon.Open()
oledbCom.Connection = oledbCon
oledbCom.CommandType = CommandType.Text
oledbCom.CommandText = "SELECT AREA_CODE FROM CSITAPPS.DAYSIN_1057"
oledbda.SelectCommand = oledbCom
oledbda.Fill(gdt)
I was able to find a solution working with a co-worker for a couple days. The issue stems from the fact that Oracle Column references are Case Sensitive. Because of this the double quotations were required, which is tricky for VB.net given quotation marks indicate and encapsulate String entries. The solution was to break the string and concatenate chr(34) into it. That in combination with ensuring that the column reference case matched what was in the table it came right up.
"SELECT " & Chr(34) & "Area_Code" & Chr(34) & " FROM CSITAPPS.DAYSIN_1057 ORDER BY " & Chr(34) & "Area_Code" & Chr(34) & " DESC;"
Why does ORDER BY work on some xlsx files and not on others?
I build SQL statements (using ADOB) to move data from closed workbooks (I need the speed and would like to use SQL to sort the data) into my active workbook. Below are the two strings I use and both work perfectly for their associated files.
mySQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] ORDER BY " & TargetSortColumn & ";" 'This works for my data dictionary.
mySQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];" 'This works for the Cost File.
I cannot find any fundamental differences between the two files. I am simply trying to copy a worksheet from each closed workbook into my current workbook and sort it with ORDER BY.
I always jump to my error trap, when using ORDER BY on my cost file. When I hit the second statement below It triggers the error trap.
Set Con = CreateObject("ADODB.Connection")
Set Data = CreateObject("ADODB.Recordset")
I have used the debugger to check and the parameters passed into this procedure are correct for each file. I pass in all strings and use the strings to build the SQL statements so there is no mismatch there.
I have opened both files and resaved them to ensure their is no basic problem with the files. I am using Excel 2010.
The worksheets in both files have their correct names passed into the subroutine correctly.
Both worksheets have data starting at cell(1,1).
Remember the two SQL statements work correctly for their respective files.
Why can I NOT use ORDER BY on some files?
I have agitated the little grey cells most vigorously on this problem, any guidance would be greatly appreciated.
I got it, but I don't know why, perhaps NULL cells crash the ORDER BY clause?
I added a WHERE clause that removed the rows that the column code <> NULL I could then use the ORDER BY code clause without difficulty.
mySQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "]
WHERE code <> NULL
ORDER BY code;
That solved the issue. I do not understand why ORDER is not working in my case with EMPTY values. I simply expected them to be at the top of the sort order.
The WHERE clause is blindingly fast as compared to the VBA equivalent.
Anyway, By using the WHERE clause the SQL works most excellency! The little grey cells are now at peace.
Craigm
I'm having some trouble expanding the way my data is logged. What I'm doing is connecting to an Oracle database, selecting the details and then outputting to a log file, delimited by pipes. The connection works, query works, but I'm only getting the first line of data written to my file. A search through similar situations and other recordset based questions does not return a solution. Below is the snippet of code that is actually doing any work:
sSQL = "select * from MONITOR.VP_EXPECTED_DETAILS"
rsOracle.open sSQL, sConnection
WHILE NOT rsOracle.EOF
sOracleOutput = rsOracle.Fields.Item(0)&"|" _
& rsOracle.Fields.Item(1)&"|" _
& rsOracle.Fields.Item(2)&"|" _
& rsOracle.Fields.Item(3)&"|" _
& rsOracle.Fields.Item(4)&"|" _
& rsOracle.Fields.Item(5)&"|" _
& rsOracle.Fields.Item(6)
rsOracle.MoveNext
WEND
oFile.WriteLine sOracleOutput
oFile.close
Move your WriteLine statement into your record loop.
Also:
While not directly related to your question, you can make your code much less verbose by omitting Fields and Item when retrieving row values. Item is the default property of Fields. And Fields is the default property of a Recordset object. Therefore, they can both be omitted.
sOracleOutput = rsOracle(0) & "|" & rsOracle(1) & "|" ...
I am importing data from an Excel spreadsheet to a VB.NET DataTable. This Excel spreadsheet has a lot of garbage data in the first 18 rows, including a lot of empty cells. I ultimately remove these rows in post-processing, but I need to access the Excel file as is, without modifying it by hand at all.
I realize that setting IMEX=1 instructs the Jet engine to assume all columns are text. However, I have an issue with setting it to another value (explained more below). So, the default Jet engine column type scan wouldn't work particularly well.
I'd like to either:
Manually define column types before the import
Force Excel to scan many more rows (I believe the default is 8) to determine the column type
However, I do have an issue with idea #2. I do not have administrative rights to open regedit.exe, so I can't modify the registry using that method. I did circumvent this before by importing a key somehow, but I can't remember how I did it. So #1 would be an ideal solution, unless someone can help me carry out idea #2.
Is this possible? Currently, I'm using the following method:
If _
SetDBConnect( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & _
";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""", True) Then
dtSchema = _dh.GetOleDbSchemaTable()
If _dh.Errors <> "" Then
Throw New Exception("::LoadFileToBuffer.GetOleDbSchemaTable::" & _dh.Errors())
End If
For Each sheetRow In dtSchema.Rows
If sheetRow("TABLE_NAME").ToString() = "TOTAL_DOLLARS$" Then
totalDollars = sheetRow("TABLE_NAME").ToString()
ElseIf sheetRow("TABLE_NAME").ToString() = "TOTAL_UNITS$" Then
totalUnits = sheetRow("TABLE_NAME").ToString()
End If
Next
'Get total dollars table
sql.Append("SELECT * FROM [" & totalDollars & "]")
dtDollars = _dh.GetTable(sql.ToString())
End If
Thank you!
You should be able to say:
sql.Append("SELECT * FROM [" & totalDollars & "$A18:X95]")
Where totalDollars is a sheet name and x95 is the last valid row. You will not be able to include headers unless they are available at row 18.
I have a function I've written that was initially supposed to take a string field and populate an excel spreadsheet with the values. Those values continually came up null. I started tracking it back to the recordset and found that despite the query being valid and running properly through the Access query analyzer the recordset was empty or had missing fields.
To test the problem, I created a sub in which I created a query, opened a recordset, and then paged through the values (outputting them to a messagebox). The most perplexing part of the problem seems to revolve around the "WHERE" clause of the query. If I don't put a "WHERE" clause on the query, the recordset always has data and the values for "DESCRIPTION" are normal.
If I put anything in for the WHERE clause the recordset comes back either totally empty (rs.EOF = true) or the Description field is totally blank where the other fields have values. I want to stress again that if I debug.print the query, I can copy/paste it into the query analyzer and get a valid and returned values that I expect.
I'd sure appreciate some help with this. Thank you!
Private Sub NewTest()
' Dimension Variables
'----------------------------------------------------------
Dim rsNewTest As ADODB.Recordset
Dim sqlNewTest As String
Dim Counter As Integer
' Set variables
'----------------------------------------------------------
Set rsNewTest = New ADODB.Recordset
sqlNewTest = "SELECT dbo_partmtl.partnum as [Job/Sub], dbo_partmtl.revisionnum as Rev, " & _
"dbo_part.partdescription as Description, dbo_partmtl.qtyper as [Qty Per] " & _
"FROM dbo_partmtl " & _
"LEFT JOIN dbo_part ON dbo_partmtl.partnum = dbo_part.partnum " & _
"WHERE dbo_partmtl.mtlpartnum=" & Chr(34) & "3C16470" & Chr(34)
' Open recordset
rsNewTest.Open sqlNewTest, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
Do Until rsNewTest.EOF
For Counter = 0 To rsNewTest.Fields.Count - 1
MsgBox rsNewTest.Fields(Counter).Name
Next
MsgBox rsNewTest.Fields("Description")
rsNewTest.MoveNext
Loop
' close the recordset
rsNewTest.Close
Set rsNewTest = Nothing
End Sub
EDIT: Someone requested that I post the DEBUG.PRINT of the query. Here it is:
SELECT dbo_partmtl.partnum as [Job/Sub], dbo_partmtl.revisionnum as Rev, dbo_part.partdescription as [Description], dbo_partmtl.qtyper as [Qty Per] FROM dbo_partmtl LEFT JOIN dbo_part ON dbo_partmtl.partnum = dbo_part.partnum WHERE dbo_partmtl.mtlpartnum='3C16470'
I have tried double and single quotes using ASCII characters and implicitly.
For example:
"WHERE dbo_partmtl.mtlpartnum='3C16470'"
I even tried your suggestion with chr(39):
"WHERE dbo_partmtl.mtlpartnum=" & Chr(39) & "3C16470" & Chr(39)
Both return a null value for description. However, if I debug.print the query and paste it into the Access query analyzer, it displays just fine. Again (as a side note), if I do a LIKE statement in the WHERE clause, it will give me a completely empty recordset. Something is really wonky here.
Here is an interesting tidbit. The tables are linked to a SQL Server. If I copy the tables (data and structure) locally, the ADO code above worked flawlessly. If I use DAO it works fine. I've tried this code on Windows XP, Access 2003, and various versions of ADO (2.5, 2.6, 2.8). ADO will not work if the tables are linked.
There is some flaw in ADO that causes the issue.
Absolutely I do. Remember, the DEBUG.PRINT query you see runs perfectly in the query analyzer. It returns the following:
Job/Sub Rev Description Qty Per
36511C01 A MAIN ELECTRICAL ENCLOSURE 1
36515C0V A VISION SYSTEM 1
36529C01 A MAIN ELECTRICAL ENCLOSURE 1
However, the same query returns empty values for Description (everything else is the same) when run through the recordset (messagebox errors because of "Null" value).
I tried renaming the "description" field to "testdep", but it's still empty. The only way to make it display data is to remove the WHERE section of the query. I'm starting to believe this is a problem with ADO. Maybe I'll rewriting it with DAO and seeing what results i get.
EDIT: I also tried compacting and repairing a couple of times. No dice.
When using ADO LIKE searches must use % instead of *. I know * works in Access but for some stupid reason ADO won't work unless you use % instead.
I had the same problem and ran accoss this forum while trying to fix it. Replacing *'s with %'s worked for me.
Description is a reserved word - put some [] brackets around it in the SELECT statement
EDIT
Try naming the column something besides Description
Also are you sure you are using the same values in the where clause - because it is a left join so the Description field will be blank if there is no corresponding record in dbo_part
EDIT AGAIN
If you are getting funny results - try a Compact/Repair Database - It might be corrupted
Well, what I feared is the case. It works FINE with DAO but not ADO.
Here is the working code:
Private Sub AltTest()
' Dimension Variables
'----------------------------------------------------------
Dim rsNewTest As DAO.Recordset
Dim dbl As DAO.Database
Dim sqlNewTest As String
Dim Counter As Integer
' Set variables
'----------------------------------------------------------
sqlNewTest = "SELECT dbo_partmtl.partnum as [Job/Sub], dbo_partmtl.revisionnum as Rev, " & _
"dbo_part.partdescription as [TestDep], dbo_partmtl.qtyper as [Qty Per] " & _
"FROM dbo_partmtl " & _
"LEFT JOIN dbo_part ON dbo_partmtl.partnum = dbo_part.partnum " & _
"WHERE dbo_partmtl.mtlpartnum=" & Chr(39) & "3C16470" & Chr(39)
Debug.Print "sqlNewTest: " & sqlNewTest
Set dbl = CurrentDb()
Set rsNewTest = dbl.OpenRecordset(sqlNewTest, dbOpenDynaset)
' rsnewtest.OpenRecordset
Do Until rsNewTest.EOF
For Counter = 0 To rsNewTest.Fields.Count - 1
MsgBox rsNewTest.Fields(Counter).Name
Next
MsgBox rsNewTest.Fields("TestDep")
rsNewTest.MoveNext
Loop
' close the recordset
dbl.Close
Set rsNewTest = Nothing
End Sub
I don't use DAO anywhere in this database and would prefer not to start. Where do we go from here?
I know some time has passed since this thread started, but just in case you're wondering, I have found out some curious about Access 2003 and the bug may have carried over to 2007 (as I can see it has).
I've had a similar problem with a WHERE clause because I needed records from a date field that also contained time, so the entire field contents would look like #6/14/2011 11:50:25 AM# (#'s added for formatting purposes).
Same issue as above, query works fine with the "WHERE ((tblTransactions.TransactionDate) Like '" & QueryDate & "*');" in the query design view, but it won't work in the VBA code using ADO.
So I resorted to using "WHERE ((tblTransactions.TransactionDate) Like '" & QueryDate & " %%:%%:%% %M');" in the VBA code, with ADO and it works just fine. Displays the record I was looking for, the trick is not to use "*" in the Like clause; or at least that was the issue in my case.
I put brackets around the word "Description" in the SELECT statement, but it's behavior remains. It works fine as long as I don't put anything in the WHERE clause. I've found if I put anything in the where clause, the description is blank (despite showing up in the Query analyzer). If I use a LIKE statement in the WHERE clause, the entire recordset is empty but it still works properly in the Query Analyzer.
Ultimately I think it's a problem with running ADO 2.8 on Vista 64
Personally I have always used DAO in Access projects.