Hello all,
Coming back for help again. I'm stumped on something that I think is simple, and have grown tired of trial and error and googling for help.
I have one main issue, and one question.
The main issue:
I am using Access 2003. Running an ODBC connection to query a linked table. This table contains dates, sales figures, information, etc. Essentially a master table. We are running one main query to pull all the data that we need; and then several local queries once this data is retrieved. I can not use local tables when querying against the ODBC linked table.
Locally, we have a table of information and account numbers. This changes all the time and can be one account number to several hundred.
The account numbers in my local table and the ODBC table are text fields. 10 characters. They are the same format.
My query is pulling all the information that I need, and I would like one of the criteria's to be my local accounts table. So I'm using a recordset and access vba.
When I change the criteria of the accounts numbers from my vba string to an actual account number. The query works fine. It will only fail when I use the string. I believe the formatting is correct, but this could be the issue.
***Please note, I'm only including the criteria portion of my query, as the main one is quite long and I know the query works when replacing the string with an actual account number.
EDIT:: I think as I'm typing this, I realize my mistake.Code:Dim myRecordset As ADODB.Recordset Dim strSQL As String Dim strOutput As String Dim strFinal As String strSQL = "SELECT ACCOUNTS FROM AccountsForForms" Set myConnection = CurrentProject.Connection Set myRecordset = CurrentDb.OpenRecordset(strSQL, DB_OPEN_DYNASET, adOpenForwardOnly) Do Until myRecordset.EOF strOutput = strOutput + myRecordset.Fields("ACCOUNTS") & ", " & vbCrLf myRecordset.MoveNext Loop strFinal = strOutput myRecordset.Close myConnection.Close Set myConnection = Nothing Set myRecordset = Nothing DoCmd.SetWarnings False 'Clears all tables first DoCmd.RunSQL "DELETE * FROM MyQueryData" strSQLQ = strSQLQ & "FROM ODBCTable WHERE ODBCTable.InvoiceDate BETWEEN #6/30/12# AND #10/27/12# AND ODBCTable.AccountNumber =" & "'" & strFinal & "'" & " AND ODBCTable.RunDate BETWEEN #8/2/12# AND #10/27/12# AND ODBCTable.CustomerType='00';" Debug.Print strSQLQ DoCmd.RunSQL strSQLQ MsgBox "Tables Updated", vbExclamation Beep End Sub
Is it not recognizing the characters in the string, because of this;
strOutput = strOutput + myRecordset.Fields("ACCOUNTS") & ", " & vbCrLf
I think this may be the issue, that this is breaking it up into individual lines. The immediate window is displaying this as well. I think my thinking was that if I was able to break up the account numbers it would read through it one by one. But I believe I just realized that was more than likely a stupid mistake.
Second question:
Date formats and strings.
Is there a correct way to use a string as a date criteria in a query?
Example: My Form has a text box with the control source being Date()-30
Is is it possible to set this date to a string, then run a query with the string as a criteria? Or is this just a terrible train of thought? I would like to have it set up in this fashion, or something similar, to avoid having to manually update the date criteria each time. I can set it so specific periods and just have it pass through to a string.
Any help/criticism or words of advice would be greatly appreciated.Code:Dim StrDte1 As String Dim StrDte2 As String Dim StrDteF1 As String Dim StrDteF2 As String Dim Dte1 As Date Dim Dte2 As Date StrDte1 = Me.Text3.Value StrDte2 = Me.Text5.Value Dte1 = CDate(Format(StrDte1, "mm/dd/yyyy")) Dte2 = CDate(Format(StrDte2, "mm/dd/yyyy")) StrDteF1 = Dte1 StrDteF2 = Dte2 strSQLQ = strSQLQ & "FROM ODBCTable WHERE ODBCTable.InvoiceDate BETWEEN '" & StrDteF1 & "' AND '" & StrDteF1 & "' AND ODBCTable.AccountNumber = '11C111E1' AND ODBCTable.RunDate BETWEEN #8/2/12# AND #10/27/12# AND ODBCTable.CustomerType='00';" Debug.Print strSQLQ DoCmd.RunSQL strSQLQ MsgBox "Tables Updated", vbExclamation Beep End Sub
Thanks again for any help or recommendations.