Passing a MS Access Form date into an Oracle SQL - oracle

I'm using MS Access to pull some data from an Oracle server via a pass-through query. The user is presented with a form in which they can input some variables (such as a date range). I would like the Oracle SQL to be able to pick up the two date fields from the form.
The current SQL (which doesn't work) is as follows:
SELECT a.I_LOAN_NUM, a.I_LOAN_SUB_ALLOC, c.N_EXCLV, e.I_GSL_SPNSR, e.N_GSL_SPNSR, b.D_CAL, b.C_LOAN_STAT, g.N_CNTRY
FROM SLD_LOAN_MSTR a
JOIN SLD_LOAN_CDL b on b.I_LOAN_ID = a.I_LOAN_ID
JOIN SLD_EXCLV c on c.I_EXCLV_ID = b.I_EXCLV_ID
JOIN SLD_AC d on d.I_AC_ID = b.I_AC_ID
JOIN SLD_CUST e on e.I_CUST_ID = d.I_CUST_ID
JOIN SLD_DPT_CNTRY f on f.I_DPT_ID = b.I_DPT_ID
JOIN SLD_CNTRY g on g.I_CNTRY_ID = f.I_CNTRY_ID
WHERE (b.C_LOAN_STAT = 'SETTLED' and b.D_CAL between [Forms]![Cost Allocation Form]![Start_Date] and [Forms]![Cost Allocation Form]![End_Date])
ORDER BY b.D_CAL
The above SQL works if I replace the form references with hard coded dates, so I know the SQL is generally good. Example:
WHERE (b.C_LOAN_STAT = 'SETTLED' and b.D_CAL between '01JAN2019' and '01FEB2019')
The error message being generated by the SQL states "ODBC--call failed. [Oracle][ODBC][Ora]ORA-00936: missing expression (#936)"
Both of the date fields in the Form are using the Short Date format.
I'm not sure if this makes any difference or not, but the Form has multiple tabs. From what I've seen from other examples, the Form reference doesn't need to take the tab labels into account.
Thanks

Pass-through queries are executed at the server. In your case the Oracle server doesn't recognize the [Forms]![Cost Allocation Form]![Start_Date] and [Forms]![Cost Allocation Form]![End_Date] attributes.
You could use VBA to dynamically update the query definition of the query to include the form control values. Then execute the query.
Dim strSQL As String
Dim qdf As QueryDef
strSQL = "SELECT a.I_LOAN_NUM, a.I_LOAN_SUB_ALLOC, c.N_EXCLV, e.I_GSL_SPNSR, e.N_GSL_SPNSR, b.D_CAL, b.C_LOAN_STAT, g.N_CNTRY " & _
"FROM SLD_LOAN_MSTR a " & _
"JOIN SLD_LOAN_CDL b on b.I_LOAN_ID = a.I_LOAN_ID " & _
"JOIN SLD_EXCLV c on c.I_EXCLV_ID = b.I_EXCLV_ID " & _
"JOIN SLD_AC d on d.I_AC_ID = b.I_AC_ID " & _
"JOIN SLD_CUST e on e.I_CUST_ID = d.I_CUST_ID " & _
"JOIN SLD_DPT_CNTRY f on f.I_DPT_ID = b.I_DPT_ID " & _
"JOIN SLD_CNTRY g on g.I_CNTRY_ID = f.I_CNTRY_ID " & _
"WHERE (b.C_LOAN_STAT = 'SETTLED' and b.D_CAL between '" & _
[Forms]![Cost Allocation Form]![Start_Date] & "' and '" & [Forms]![Cost Allocation Form]![End_Date] & _
"' ORDER BY b.D_CAL"
Set qdf = CurrentDb.QueryDefs("PassThroughQueryName")
qdf.SQL = strSQL
Also since your using dates, I would suggest you format your dates to the ISO format yyyy-mm-dd
in this case formatting the controls like
Format([Forms]![Cost Allocation Form]![Start_Date], "yyyy-mm-dd") & "' and '" & Format([Forms]![Cost Allocation Form]![End_Date], "yyyy-mm-dd")
Another way would be to just use VBA ADO to access the Oracle Server and retrieve the data. You would still need to build up your SQL string as mentioned here.

Related

Query returns only 4 out of 12 values of my table. Why is this happening?

I have created a script which I am using on 2 different system. One is Windows 10 with SQL Server Express 2019 and the other is on Windows 7 (32bit) with SQL Server Express 2014. The db I am using has the same structure. The code I am using in ASP Classic is exactly the same on both machines! But in the win7 machine does not work as it should!
I am having a query to my db and the problem is that SOME of the values I am getting are empty. The code I am using is the following:
<%
Set rs21 = Server.CreateObject("ADODB.Recordset")
strSQL21 = "SELECT * FROM otherfiles WHERE animalid LIKE '" & lngRecordNo & "'
AND historyid LIKE '" & historyid & "' order by datedone DESC"
rs21.Open strSQL21, adoCon
%>
BLAH BLAH BLAH
<%
Do While not rs21.EOF
historyid = rs21("historyid")
if not historyid = "" then
Set rshis = Server.CreateObject("ADODB.Recordset")
strSQLhis = "SELECT * FROM history WHERE id_no LIKE '" & historyid & "' order by id_no DESC"
rshis.Open strSQLhis, adoCon
'------------------- if I remove the following line (Set rscol) it works fine ---------------------------
Set rscol = Server.CreateObject("ADODB.Recordset")
strcol = "SELECT * FROM hospitals WHERE id_no = "& rsGuestbook21("hosid") &" order by id_no DESC"
rscol.Open strcol, adocon
color = rscol("color")
%>
BLAH BLAH BLAH
<%=rs21("datedone")%> ---> empty value
<%=rs21("id_no")%> ---> works fine!!!
I am 100% sure that my table has its values. In fact as I mention above only but setting the rscol = Server.CreateObject("ADODB.Recordset") then the values becomes ??empty??. Commenting this line works fine.
I just figured out my ??mistake??...
Not really sure if it is a mistake thou... but the problem solved when I placed a field that had a DATA TYPE of "nvarchar(MAX)" at the end of my of my table! Weird stuff??? Do you think that there is something more than that in which I should investigate?

How to populate a data report row by row

I have to change a very old application written in Vb6 with Data Report.
Actually displays a query records with only one field named txtdata.
Now I would display always one field (txtdata) but with different text size and style depending on whether it starts with a # or not.
I thought to check records for records if it starts with # or not and set text style and size for each record.
How can I do?
Dim rs As Adodb.Recordset
Dim sql As String
Dim regtratt As
.................
' I have a Data Report with textbox named txtdata in Section1
sql = ""
sql = sql + "SELECT txtdata "
sql = sql + "FROM journal "
sql = sql + "WHERE cod=421 "
sql = sql + "ORDER BY id "
Set rs = xOpenRecordset(sql, cnOnline)
If rs.RecordCount > 0 Then
Set regtratt.DataSource = rs
regtratt.DataMember = ""
' I want to change the font according to the text but I don't know how to because DataSource=rs takes the records.
'If InStr("#", regtratt.Sections("Section1").Controls(1).Item.Value) Then
' regtratt.Sections("Section1").Controls(1).Font.size = 20
' regtratt.Sections("Section1").Controls(1).Font.Bold = True
'End If
regtratt.Orientation = rptOrientLandscape
regtratt.WindowState = vbMaximized
regtratt.Font.Name = "courier new"
regtratt.Refresh
regtratt.Show vbModal
Unload regtratt
Else
MsgBox ("Sorry No data")
End If

Populate and sort a ComboBox from another ComboBox, VBA

Hi Everyone I am having trouble getting a ComboBox to sort, when another combobox is selected.
I think I have the Right SQL Syntax but I cant seem to get the vba to run it through; currently the vba returns all of the states in the recordset regardless of the company.
Private Sub CboCountry_Click()
Set db = CurrentDb
Dim SQLStr As String
Set RsState = db.OpenRecordset("T2States", dbOpenSnapshot, dbSeeChanges)
'populates combobox with recordset, that is defined by the country input from the form
RsState.MoveFirst
Do While Not RsState.EOF
Me.CboState.RowSource = Me.CboState.RowSource & RsState("StateID") & ";" & RsState("State") & ";"
RsState.MoveNext
Loop
I think this is the right SQL String but I'm having trouble to get it to work.
'SQLStr = "SELECT T2States.StateID, T2States.States, T2States.CountryID" & _
" FROM T2States GROUP BY T2States.StatesID" & _
" WHERE T2States.CountryID = """ & Me.CboCountry.Value & """"
Any help will be greatly appreciated.
Edit#1
See Full Code below, the error that pops up when I substitute SQLStr into the Openrecordset is a Run-time error '3078' the microsoft access database engine cannot find the input table or query 'SQLStr'. Make sure it exists and that its name is spelled correctly.
What should happen is when a country is selected from CboCountry combobox, it will load the CboState combobox by sorting the recordset by CountryID
see below for both code parts
Private Sub Form_Load()
Set db = CurrentDb
Set RsCompany = db.OpenRecordset("T1Company", dbOpenDynaset, dbSeeChanges)
Set RsCountry = db.OpenRecordset("T2Countries", dbOpenSnapshot, dbSeeChanges)
Set RsAddress = db.OpenRecordset("T1Addresses", dbOpenDynaset, dbSeeChanges)
Set RsAddressType = db.OpenRecordset("T2AddressType", dbOpenSnapshot, dbSeeChanges)
Set RsCompanyAddress = db.OpenRecordset("T3Company_Address", dbOpenDynaset, dbSeeChanges)
Me.CboCountry = Null
Me.TxtAddress1 = Null
Me.TxtAddress2 = Null
Me.TxtAddress3 = Null
Me.TxtCity = Null
Me.CboAddressType = Null
Me.CboCountry = Null
Me.CboState = Null
Me.TxtPostalCode = Null
Me.TxtCompanyID = Null
Me.TxtLegalName = Null
Me.TxtNickname = Null
Me.TxtAddressID = Null
RsCountry.MoveFirst
Do While Not RsCountry.EOF
Me.CboCountry.RowSource = Me.CboCountry.RowSource & RsCountry("CountryID") & ";" & RsCountry("Country") & ";"
RsCountry.MoveNext
Loop
RsAddressType.MoveFirst
Do While Not RsAddressType.EOF
Me.CboAddressType.RowSource = Me.CboAddressType.RowSource & RsAddressType("AddressTypeID") & ";" & RsAddressType("AddressType") & ";"
RsAddressType.MoveNext
Loop
Me.TxtLegalName.SetFocus
End Sub
Private Sub CboCountry_Click()
Set db = CurrentDb
Dim SQLStr As String
'SQLStr = "SELECT T2States.StateID, T2States.State, T2States.CountryID" & _
" FROM T2States" & _
" WHERE T2States.CountryID = """ & Me.CboCountry.Value & """"
Set RsState = db.OpenRecordset("T2States", dbOpenDynaset, dbSeeChanges)
'populates combobox with recordset, that is defined by the country input from the form
RsState.MoveFirst
Do While Not RsState.EOF
Me.CboState.RowSource = Me.CboState.RowSource & RsState("StateID") & ";" & RsState("State") & ";"
RsState.MoveNext
Loop
End Sub
Let us see
1- sure you've to append with
Having T2States.States, T2States.CountryID
2- Error exist in it, extra 's' in the Column name:
GROUP BY T2States.StatesID
3- put all the code and i'll check with you what you miss.
best regards
This one turned out to be a quick fix in the Property Sheet under the DATA tab, the Row Source Type had to be changed back to 'Table/Query' from a 'Value'.
There is VBA that could account for this but it was just a simple as changing that Row Source.
The Reason for the mix up, for a quick bit of background if it helps, is that all my combo boxes are unbound and I was binding them with VBA Recordsets so the rowsource has to be a value list - Essentially the VBA is writing the list everytime it loads.
Where as when I started using SQL to generate the recordset, even though it was in VBA I had to change the property back to Table/Query.
Thanks.

Report: Counting number of records, between a number range

MS Access 2013. I need to write a report that shows the number of books inside our storage boxes.
All books purchased are given a unique nbr (SaleID). We store 25 books per box, and 100 books takes 4 boxes. As we sell books, they are removed from their specific boxes, and over time, lets say 30 books are sold. We can then discard one box, and store the remaining 70 books in three boxes. We have thousands of books and hundreds of storage boxes. I need to run a report that shows me how many books are in stock, per every 100 books, so that I can reduce the number of boxes (and save storage space).
The report should show like this:
001 - 100 34 (i.e. 34 books are in stock in the number range 001 to 100.
101 - 200 35 (35 books in stock)
201 - 300 22 (22 books in stock)
301 - 400 60 (60 books in stock)
etc
The query below does what I want, where I enter the criteria of the hundred books being queried. But I need a line on the report, for each hundred books, for all the books we hold.
SELECT tblSale.BookInStock, Count(tblSale.BookInStock) AS [Total Books]
FROM tblSale
WHERE (((tblSale.SaleID) Between 4201 And 4300))
GROUP BY tblSale.BookInStock
HAVING (((tblSale.BookInStock)=Yes));
If someone has any ideas I'd be grateful for your thoughts
Cheers
Nev
Since you have to query every hundred items, coding will be needed to run through every per hundred batch. Therefore, consider creating a temp table from a VBA looped query.
First iteration of loop creates the table replacing previous, then all other iterations append by hundred batches (notice step used to skip iterator forward) across your thousands. By the way, your HAVING clause condition was moved to WHERE clause.
Dim i As Long
Dim sqlStr As String
Dim db As DAO.Database
Dim tbl As TableDef
Set db = CurrentDb
For Each tbl in db.TableDefs
If tbl.Name = "BoxBooksPerHundred" Then
db.TableDefs.Delete(tbl.Name)
End If
Next tbl
For i = 1 to 4300 Step 100 ' ADJUST AS NEEDED
If i = 1 Then
' MAKE-TABLE QUERY
strsql= "SELECT '" & i & " - " & i + 100 & "' AS [HundredRange], tblSale.BookInStock," _
& " Count(tblSale.BookInStock) AS [Total Books]" _
& " INTO BoxBooksPerHundred FROM tblSale" _
& " WHERE (((tblSale.BookInStock)=Yes)) And" _
& " (((tblSale.SaleID) Between " & i & " And " & i + 100 & "))" _
& " GROUP BY '" & i & " - " & i + 100 & "', tblSale.BookInStock;"
db.Execute strSQL, dbFailOnError
Else
' APPEND QUERY
strsql= "INSERT INTO BoxBooksPerHundred (HundredRange, BookInStock, [Total Books])"
& " SELECT '" & i & " - " & i + 100 & "' AS [HundredRange], tblSale.BookInStock," _
& " Count(tblSale.BookInStock) AS [Total Books]" _
& " FROM tblSale" _
& " WHERE (((tblSale.BookInStock)=Yes)) And" _
& " (((tblSale.SaleID) Between " & i & " And " & i + 100 & "))" _
& " GROUP BY '" & i & " - " & i + 100 & "', tblSale.BookInStock;"
db.Execute strSQL, dbFailOnError
End If
Next i
Set tbl = Nothing
Set db = Nothing
Run this loop on the trigger event that opens report. But since quite a bit of data, try triggering on database open if you expect data not to change often. Ultimately, use this temp table, BooksPerHundred, as recordsource for report.

iteration through outlook appointments

I know how to iterate through non-recurring appointments in Outlook.
My question is, how to I iterate through Outlook appointments including the recurring appointments?
Thank you.
If you are open to using 3rd party libraries, I'd suggest using "Redemption" library (http://www.dimastr.com/redemption/). This library has useful RDOFolder2 interface with GetActivitiesForTimeRange method.
Here you can find more information about usage of this interface:
(http://www.dimastr.com/redemption/rdo/rdofolder.htm)
If you don't want to use 3rd party library and need to stick to Outlook API, the trick is to set IncludeRecurrences flag to true before iterating appointments. The following article should provide enough information on how to do that:
(http://www.outlookcode.com/article.aspx?id=30)
Actually there is no need to use third party tools. There is the option IncludeRecurrences which takes care of this:
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set MyFolder = myNameSpace.GetDefaultFolder(olFolderCalendar)
Set oItems = MyFolder.Items
' Restrict Date
strFilter = "[Start] >= " + "'" + ourStart + "'"
Set oItems = oItems.Restrict(strFilter)
strFilter = "[End] <= " + "'" + ourEnd + "'"
Set oItems = oItems.Restrict(strFilter)
' Restrict Category
strFilter = "[Categories] = " + "'" + ourCategory + "'"
Set oItems = oItems.Restrict(strFilter)
oItems.Sort "[Start]"
' We want recurring, too (http://www.pcreview.co.uk/forums/get-recurring-appointment-dates-vba-t799214.html)
oItems.IncludeRecurrences = True

Resources