In Excel 2003 I'm getting a Runtime error 1004: "application-defined or object-defined error" on the last line of this code (commandtext = abc)
Sub SCommandTxt()
Dim abc as string
abc = Sheets("Totals").PivotTables("PivotTable2").PivotCache.CommandText
Sheets("Totals").PivotTables("PivotTable2").PivotCache.CommandText = abc
End Sub
This isn't really what I'm trying to do, but not knowing what is causing an error in something as simple as this is driving me up a wall. The Pivot table at hand is an ODBC connection. The following code was run before this code and works fine. All I really want to do is change the query dynamically based on the changing range "WhereFilters". The below query works alright, but I'd prefer not to have to unhide and select the sheet and go through the pivotwizard if I can just change the commandText directly (though based on the errors I'm getting maybe not... Though others seem to think the above is possible, so I don't know why it isn't working for me):
Sub UpdatePvt()
Dim DBDir As String, DBName As String, SortType As String, Size As String
Dim QueryArry1(0 To 100) As String, rng As Range, x As Integer
DBDir = "C:\Documents and Settings\jt\"
DBName = "DatabaseExample.mdb"
If Range("ComboResult1") = 1 Then
SortType = "TDollars"
Sheets("Totals").PivotTables("PivotTable1").PivotFields("DIV_ID").AutoSort _
xlDescending, "Sum of Dollars"
Sheets("Totals").PivotTables("PivotTable2").PivotFields("DIV_ID").AutoSort _
xlDescending, "Sum of Dollars"
Else
SortType = "TCounts"
Sheets("Totals").PivotTables("PivotTable1").PivotFields("DIV_ID").AutoSort _
xlDescending, "Sum of Counts"
Sheets("Totals").PivotTables("PivotTable2").PivotFields("DIV_ID").AutoSort _
xlDescending, "Sum of Counts"
End If
If Range("ComboResult2") = 1 Then
Size = "Total"
ElseIf Range("ComboParOUT") = 2 Then
Size = "Small"
Else
Size = "Large"
End If
QueryArry1(0) = "SELECT Top 500 C.* "
QueryArry1(1) = "FROM Final03 C "
x = 2
If Not (Range("NoFilters")) Then
QueryArry1(x) = "INNER JOIN (Select DIV_ID FROM FullLookup WHERE "
x = x + 1
For Each rng In Range("WhereFilters")
QueryArry1(x) = rng.Value
x = x + 1
Next rng
QueryArry1(x) = "GROUP BY DIV_ID) E ON C.DIV_ID = E.DIV_ID "
x = x + 1
End If
QueryArry1(x) = "WHERE C.EntitySize = '" & Size & "' "
QueryArry1(x + 1) = "ORDER BY C." & SortType & " DESC "
'Example Query Results:
'SELECT Top 500 C.* FROM Final03 C INNER JOIN (Select DIV_ID FROM FullLookup WHERE Year = 2008 and State = 'MN' and Type = 'RST44' GROUP BY DIV_ID) E ON C.DIV_ID = E.DIV_ID WHERE C.EntitySize = 'Large' ORDER BY C.TCounts DESC
Sheets("Totals").Visible = xlSheetVisible
Sheets("Totals").Select
Sheets("Totals").PivotTables("PivotTable1").DataBodyRange.Select
Sheets("Totals").PivotTableWizard SourceType:=xlExternal, _
SourceData:=QueryArry1, _
Connection:=Array( _
Array("ODBC;DSN=MS Access Database;DBQ=" & DBDir & "\" & DBName & ";"), _
Array("DefaultDir=" & DBDir & ";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;") _
)
Sheets("Totals").PivotTables("PivotTable2").DataBodyRange.Select
Sheets("Totals").PivotTableWizard _
SourceType:=xlPivotTable, _
SourceData:="PivotTable1"
Sheets("Totals").Visible = xlSheetHidden
End Sub
Thanks
Your problem appears to be the exact one described here:
Limitation of PivotCache.CommandText property
How long is the string you're trying to set as CommandText?
Related
I have an Access database to report on event statistics gathered from a mainframe system. The mainframe scheduler (ZEKE) doesn't have robust reporting features, so I export daily event data to report on.
A master listing from a separate source (a static list that will not change on a regular basis) lists the individual applications, including the application code (which is the naming standard for production runs) and the name of the programmer, coordinator, manager, business unit, etc. for that application.
The user can search by any field, application code, programmer, coordinator, etc.
Choose the production center to search in (there are 5) or default to all, and choose all dates, a single date, or a date range.
The query takes the search parameters and starting with either the application code, or the person, searches the table for applications and copies records to a temp table for reporting.
For example, to see how many failures the application coordinator John Doe had for the past week for all of the applications he is responsible for, the query would move all application records listing John Doe as the coordinator to the temp table.
From there, it moves through the temp table for each application and searches the event data for events under that application code which meet the criteria entered for date, production center and event type (success, failure or both).
This is moved to a temp table for the final report.
The table for event data is currently 2.5 million lines (this is 15 days worth of data) and is growing daily.
I put the back end onto a newly created NAS drive on our network.
A report that took two minutes when the back end and front end were on the same machine now takes 29 minutes.
Any suggestions to streamline the queries over a network?
Code which is run from the report criteria selection form and runs the report.
'this macro will generate a report based on multiple input criteria.
'this report allows the user to slect:
' date range, single date or all dates
' type of events: Abends, Successes or both
' centers to pull data from: OCC,QCC,BCC,ITS,DAIN, or ALL centers
' The type of data to report on: App code, App Coordinator, Custodian, L3, L4 or L5
'Once the user has selected all of the required data and fields, the report will be generated
'based on the selection criteria.
'we begin by defining the active database as the currently open database
Dim db As DAO.Database
Set db = DBEngine(0)(0)
On Error GoTo ErrorHandler
'Now we designate the variables which will be used in this macro
Dim strSQ1 As String
Dim strSQ2 As String
Dim strSQ3 As String
Dim strSQ4 As String
Dim appl As String
Dim evstatus As String
Dim appletype As String
Dim fullapp As String
Dim length As Long
Dim iipmname As String
Dim iipmcoor As String
Dim fullappnm As String
Dim fullappcoor As String
Dim kinddate As String
Dim coor As String
Dim cust As String
Dim appL3 As String
Dim appL4 As String
Dim appL5 As String
Dim ctrOCC As String
Dim ctrMTL As String
Dim ctrBCC As String
Dim ctrITS As String
Dim ctrDAIN As String
'We will start by setting some default values
'We will ste the default values for center selection.
'We start by searching for terms we know are not there, then change them to
'valid search terms if the center is selected.
ctrOCC = "notOCC"
ctrMTL = "notMTL"
ctrBCC = "notBCC"
ctrITS = "notITS"
ctrDAIN = "notUSWM"
fullapp = "*"
'First we determine which event types the user wants to look for
state = Me![opt-status].Value
If state = 1 Then
evstatus = " [ev-status] = 'AEOJ'"
ElseIf state = 2 Then
evstatus = " [ev-status] = 'EOJ'"
ElseIf state = 3 Then
evstatus = " ([ev-status] = 'EOJ' OR [ev-status] = 'AEOJ')"
End If
'MsgBox "Event status pulled is:.. " & evstatus & "."
' Next up we will configure the date parameters based on the user input
If [grp-datesel] = 1 Then
Sdte = "1"
Edte = "9999999"
kinddate = "[ev-date] >= " & Sdte & " AND [ev-date] <= " & Edte & " "
End If
If [grp-datesel] = 2 Then
'error handling
If IsNull(Me.[sel-onedate]) Then
MsgBox "You have not entered a date to search....please try again."
Me.[sel-onedate] = Null
Me.[sel-onedate].SetFocus
Exit Sub
End If
'end of error handling
Dim currdte As Date
currdte = Me![sel-onedate].Value
currjul = Format(currdte, "yyyyy")
daycurr = CDbl(currjul)
Sdte = daycurr
Edte = daycurr
kinddate = "[ev-date] >= " & Sdte & " AND [ev-date] <= " & Edte & " "
End If
If [grp-datesel] = 3 Then
'error handling
If IsNull(Me.[sel-Sdate]) Or IsNull(Me.[sel-Edate]) Then
MsgBox "You Must enter a start and end date for the search....please try again."
Me.[sel-Sdate] = Null
Me.[sel-Edate] = Null
Me.[sel-Sdate].SetFocus
Exit Sub
End If
'end of error handling
Dim startdte As Date
Dim enddte As Date
startdte = Me.[sel-Sdate].Value
enddte = Me.[sel-Edate].Value
startjul = Format(startdte, "yyyyy")
endjul = Format(enddte, "yyyyy")
Sday = CDbl(startjul)
Eday = CDbl(endjul)
Sdte = Sday
Edte = Eday
'MsgBox "start date is " & Sdte & " and end date is " & Edte & "."
'check that dates are in proper chronological order
If Sdte > Edte Then
MsgBox "The start Date you entered is after the end date....please try again."
Me.[sel-Sdate] = Null
Me.[sel-Edate] = Null
Me.[sel-Sdate].SetFocus
Exit Sub
End If
'keep going if it's all good
kinddate = "[ev-date] >= " & Sdte & " AND [ev-date] <= " & Edte & " "
End If
MsgBox "Date used is:.. " & kinddate & "."
'Now lets look at center selection
If [chk-allctr].Value = True Then
ctrOCC = "OCC"
ctrMTL = "MTL"
ctrBCC = "BCC"
ctrITS = "ITS"
ctrDAIN = "USWM"
End If
If [chk-OCC].Value = True Then
ctrOCC = "OCC"
End If
If [chk-MTL].Value = True Then
ctrMTL = "MTL"
End If
If [chk-BCC].Value = True Then
ctrBCC = "BCC"
End If
If [chk-RTF].Value = True Then
ctrITS = "ITS"
End If
If [chk-DAIN].Value = True Then
ctrDAIN = "DAIN"
End If
'Error handling if no center is selected
If [chk-OCC].Value = Flase Then
If [chk-MTL].Value = Flase Then
If [chk-BCC].Value = Flase Then
If [chk-RTF].Value = Flase Then
If [chk-DAIN].Value = Flase Then
MsgBox "You have not selected a center to search search....please try again."
Me.[chk-allctr].SetFocus
Exit Sub
End If
End If
End If
End If
End If
'end of error handling
'MsgBox "centers used are: Chr(10) " & ctrOCC & " Chr(10) " & ctrBCC & " Chr(10) " & ctrMTL & " Chr(10) " & ctrITS & " Chr(10) " & ctrDAIN & " For this run"
'All good so far, now we will parse the application code if an
'application code report is selected
appl = "*"
If [opt-criteria].Value = 1 Then
'error handling
If IsNull(Me.[sel-appcode]) Then
MsgBox "You have not entered an application code to search....please try again."
Me.[sel-appcode] = Null
Me.[sel-appcode].SetFocus
Exit Sub
End If
'end of error handling
End If
If [opt-criteria].Value = 1 Then
appl = Me![sel-appcode].Value
End If
'trust = "no"
'If Mid(appl, 3, 2) = "RT" Then trust = "yes"
'length = Len(appl)
'If length = 2 Then appltype = "short"
'If length = 3 Then appltype = "long"
'If appltype = "short" Then fullapp = "" & appl & "00"
'If appltype = "long" Then fullapp = "" & appl & "0"
'If trust = "yes" Then fullapp = appl
'End If
fullapp = appl
'MsgBox "App to use is: " & appl & " fullapp code is " & fullapp & "."
'Now we set values if names are used
coor = "*"
cust = "*"
appL3 = "*"
appL4 = "*"
appL5 = "*"
If [opt-criteria].Value = 2 Then
'error handling
If IsNull(Me.[sel-coor]) Then
MsgBox "You have not entered a Coordinator to search....please try again."
Me.[sel-coor] = Null
Me.[sel-coor].SetFocus
Exit Sub
End If
'end of error handling
coor = Me![sel-coor].Value
'MsgBox "Coordinator report selected for: " & coor & "."
End If
If [opt-criteria].Value = 3 Then
'error handling
If IsNull(Me.[sel-custodian]) Then
MsgBox "You have not entered a Custodian to search....please try again."
Me.[sel-custodian] = Null
Me.[sel-custodian].SetFocus
Exit Sub
End If
'end of error handling
cust = Me![sel-custodian].Value
'MsgBox "Custodian report selected for: " & cust & "."
End If
If [opt-criteria].Value = 4 Then
'error handling
If IsNull(Me.[sel-L3]) Then
MsgBox "You have not entered an L3 to search....please try again."
Me.[sel-L3] = Null
Me.[sel-L3].SetFocus
Exit Sub
End If
'end of error handling
appL3 = Me![sel-L3].Value
'MsgBox "L3 report selected for: " & appL3 & "."
End If
If [opt-criteria].Value = 5 Then
'error handling
If IsNull(Me.[sel-L4]) Then
MsgBox "You have not entered an L4 to search....please try again."
Me.[sel-L4] = Null
Me.[sel-L4].SetFocus
Exit Sub
End If
'end of error handling
appL4 = Me![sel-L4].Value
'MsgBox "L4 report selected for: " & appL4 & "."
End If
If [opt-criteria].Value = 6 Then
'error handling
If IsNull(Me.[sel-L5]) Then
MsgBox "You have not entered an L5 to search....please try again."
Me.[sel-L5] = Null
Me.[sel-L5].SetFocus
Exit Sub
End If
'end of error handling
appL5 = Me![sel-L5].Value
'MsgBox "L5 report selected for: " & appL5 & "."
End If
'Most of these reports take a while to build with this macro, so to make sure the user
'knows that the macro is still working, we didsplay a splash screen. It's cute and has
'hamsters, cause everyone loves hamsters.
DoCmd.OpenForm "PlsWaitFrm", acWindowNormal
[Forms]![PlsWaitFrm].Repaint
'All of out criteria values are now selected. We can move on to pulling data from the tables.
'We start by populating the IIPM table with the information that we require for applications.
strSQ1 = "DELETE * from [tbl-RPT-IIPM] "
db.Execute strSQ1
strSQ2 = "INSERT INTO [tbl-RPT-IIPM] " & _
"SELECT * FROM [tbl-IIPM] " & _
"WHERE (([AppCode] like '" & fullapp & "')" & _
"AND ([AppCoordinator] like '" & coor & "') " & _
"AND ([AppCustodian] like '" & cust & "') " & _
"AND ([L3] like '" & appL3 & "') " & _
"AND ([L4] like '" & appL4 & "') " & _
"AND ([L5] like '" & appL5 & "')) "
db.Execute strSQ2
'MsgBox "made it past the populate of rpt-iipm"
'Now we have populated the IIPM report table, it's time to populate the event report table.
'We will loop through all fields in the IIPM report table and pull information for each
'application code.
strSQ3 = "DELETE * from [tbl-EVENTREPORT] "
db.Execute strSQ3
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl-RPT-IIPM") 'this opens the IIPM report table just populated
'populate the table
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
'we will execute these action against the selected record.
'first step - parse the application code to display the full application code
appl = rs![AppCode].Value
length = Len(appl)
If length = 1 Then appl = "" & appl & "00"
rptdelin = Mid(appl, 3, 1)
rptcode = Mid(appl, 1, 3)
If rptdelin = "0" Then rptcode = Mid(appl, 1, 2)
If rptdelin = "R" Then rptcode = "RT" & Mid(appl, 1, 2) & ""
'MsgBox "searching for: " & rptcode & "."
applist = applist & "," & appl
strSQ4 = "INSERT INTO [tbl-EVENTREPORT] " & _
"SELECT * FROM [tbl-EVENT DATA] " & _
"WHERE (([ev-jobname] LIKE '?" & rptcode & "*') " & _
"AND (([ev-ctr] = '" & ctrOCC & "')" & _
"OR ([ev-ctr] = '" & ctrMTL & "')" & _
"OR ([ev-ctr] = '" & ctrBCC & "')" & _
"OR ([ev-ctr] = '" & ctrITS & "')" & _
"OR ([ev-ctr] = '" & ctrDAIN & "'))" & _
"AND (" & kinddate & ") " & _
"AND " & evstatus & ")"
db.Execute strSQ4
'now we're done with this report, we move on to the next
rs.MoveNext 'press Ctrl+G to see debuG window beneath
Loop
'END OF LOOPING CODE
'MsgBox "made it past the looping"
'Now we have completed populating the table that the report will be based on.
'Next step is to gather master statistics to produce abend and success percentages.
totfail = DCount("[ev-status]", "tbl-EVENTREPORT", "[ev-status] = 'AEOJ'")
totsucc = DCount("[ev-status]", "tbl-EVENTREPORT", "[ev-status] = 'EOJ'")
Dim allabend As Long
Dim allsucc As Long
allabend = DCount("[ev-status]", "[tbl-EVENT DATA]", "[ev-status] = 'AEOJ' AND ([ev-date] >= " & Sdte & " AND [ev-date] <= " & Edte & ")")
allsucc = DCount("[ev-status]", "[tbl-EVENT DATA]", "[ev-status] = 'EOJ' AND ([ev-date] >= " & Sdte & " AND [ev-date] <= " & Edte & ")")
Dim pctabend As Long
Dim pctsucc As Long
pctabend = (totfail / allabend) * 100
pctsucc = (totsucc / allsucc) * 100
'Now we will generate the reports for display based on what type of report was selected
'by the user in the initial form.
'Before we open the report, we will close the splash screen
DoCmd.Close acForm, "PlsWaitFrm", acSaveNo
'Now we open the report
If [opt-criteria].Value = 1 Then
fullappnm = DLookup("AppName", "tbl-RPT-IIPM", "AppCode = '" & fullapp & "' ")
fullappcoor = DLookup("AppCoordinator", "tbl-RPT-IIPM", "AppCode = '" & fullapp & "' ")
DoCmd.OpenReport "rpt-APPLREPORT", acViewReport
[Reports]![rpt-APPLREPORT]![rpt-appcode].Value = fullapp
[Reports]![rpt-APPLREPORT]![rpt-appname].Value = fullappnm
[Reports]![rpt-APPLREPORT]![rpt-appcoor].Value = fullappcoor
[Reports]![rpt-APPLREPORT]![rpt-abendtot].Value = totfail
[Reports]![rpt-APPLREPORT]![rpt-succtot].Value = totsucc
[Reports]![rpt-APPLREPORT]![rpt-abdpct].Value = pctabend
[Reports]![rpt-APPLREPORT]![rpt-succpct].Value = pctsucc
End If
If [opt-criteria].Value = 2 Then
DoCmd.OpenReport "rpt-COORREPORT", acViewReport
[Reports]![rpt-COORREPORT]![rpt-appcode].Value = applist
[Reports]![rpt-COORREPORT]![rpt-appcoor].Value = coor
[Reports]![rpt-COORREPORT]![rpt-abendtot].Value = totfail
[Reports]![rpt-COORREPORT]![rpt-succtot].Value = totsucc
[Reports]![rpt-COORREPORT]![rpt-abdpct].Value = pctabend
[Reports]![rpt-COORREPORT]![rpt-succpct].Value = pctsucc
End If
If [opt-criteria].Value = 3 Then
DoCmd.OpenReport "rpt-CUSTREPORT", acViewReport
[Reports]![rpt-CUSTREPORT]![rpt-appcode].Value = applist
[Reports]![rpt-CUSTREPORT]![rpt-appcoor].Value = cust
[Reports]![rpt-CUSTREPORT]![rpt-abendtot].Value = totfail
[Reports]![rpt-CUSTREPORT]![rpt-succtot].Value = totsucc
[Reports]![rpt-CUSTREPORT]![rpt-abdpct].Value = pctabend
[Reports]![rpt-CUSTREPORT]![rpt-succpct].Value = pctsucc
End If
If [opt-criteria].Value = 4 Then
DoCmd.OpenReport "rpt-L3REPORT", acViewReport
[Reports]![rpt-L3REPORT]![rpt-appcode].Value = applist
[Reports]![rpt-L3REPORT]![rpt-appcoor].Value = appL3
[Reports]![rpt-L3REPORT]![rpt-abendtot].Value = totfail
[Reports]![rpt-L3REPORT]![rpt-succtot].Value = totsucc
[Reports]![rpt-L3REPORT]![rpt-abdpct].Value = pctabend
[Reports]![rpt-L3REPORT]![rpt-succpct].Value = pctsucc
End If
If [opt-criteria].Value = 5 Then
DoCmd.OpenReport "rpt-L4REPORT", acViewReport
[Reports]![rpt-L4REPORT]![rpt-appcode].Value = applist
[Reports]![rpt-L4REPORT]![rpt-appcoor].Value = appL4
[Reports]![rpt-L4REPORT]![rpt-abendtot].Value = totfail
[Reports]![rpt-L4REPORT]![rpt-succtot].Value = totsucc
[Reports]![rpt-L4REPORT]![rpt-abdpct].Value = pctabend
[Reports]![rpt-L4REPORT]![rpt-succpct].Value = pctsucc
End If
If [opt-criteria].Value = 6 Then
DoCmd.OpenReport "rpt-L5REPORT", acViewReport
[Reports]![rpt-L5REPORT]![rpt-appcode].Value = applist
[Reports]![rpt-L5REPORT]![rpt-appcoor].Value = appL5
[Reports]![rpt-L5REPORT]![rpt-abendtot].Value = totfail
[Reports]![rpt-L5REPORT]![rpt-succtot].Value = totsucc
[Reports]![rpt-L5REPORT]![rpt-abdpct].Value = pctabend
[Reports]![rpt-L5REPORT]![rpt-succpct].Value = pctsucc
End If
ErrorHandler:
If Err.Number = 7874 Then
Resume Next 'Tried to delete a non-existing table, resume
End If
End Sub
'''
Firstly, you need to work out where the bottlenecks are, so I would suggest putting some Debug.Print Now statements throughout the code to give you an idea of what is causing the issue.
I would guess that two of the processes that take most of the time are the DELETE/INSERT statements that you are doing.
I would suggest that rather than doing this, you look at normalizing your database, and then creating a query that provides the information that you need.
Also, by running the report directly from a query rather than a temporary table means that you don't have to worry about the deletes/inserts creating database bloat.
If you really insist on keeping this process, then consider deleting the table [tbl-RPT-IIPM] and then recreating it, rather than deleting the records. And consider removing the indexes before the insert, and then adding them back afterwards, as indexes splow down inserts, but obviously speed up searches and joins.
Also, when you are inserting data into [tbl-RPT-IIPM], you are using ([L3] like '" & appL3 & "'), which is the same as ([L3]='" & appL3 & "'), but slower.
When you are inserting data into [tbl-EVENTREPORT], you are doing it when looping through a recordset - it may be faster to use an INSERT SQL statement.
Regards,
OK, with some more information, some more answers that may (or may not!!) help. Again, you will need to run timing tests to see which works best for you.
Try adding a "Yes/No" field to the table [tbl-EVENT DATA]. You can then use an UPDATE statement to indicate which fields to include in the report, rather than using the slow INSERT query.
Another thing to try would be to replace the INSERT statement with several, each using a different value for [ev-ctr]. Or else rather than using OR try using IN:
strSQ4 = "INSERT INTO [tbl-EVENTREPORT] " & _
"SELECT * FROM [tbl-EVENT DATA] " & _
"WHERE [ev-jobname] LIKE '?" & rptcode & "*' " & _
"AND [ev-ctr] IN('" & ctrOCC & "','" & ctrMTL & "','" & ctrBCC & "','" & ctrITS & "','" & ctrDAIN & "')" & _
"AND " & kinddate & _
"AND " & evstatus
Also, I notice that kinddate is set to effectively include all dates in one instance, and that evstatus is set to include both "EOJ" and "AEOJ" in one instance. Rather than including these fields as criteria in these cases, you may wish to not include them at all:
If state = 1 Then
evstatus = " AND [ev-status] = 'AEOJ'"
ElseIf state = 2 Then
evstatus = " AND [ev-status] = 'EOJ'"
ElseIf state = 3 Then
evstatus = " "
End If
And then you would rewrite " AND " & evstatus to & evstatus in the SQL statement.
A final thing to look at is to actually run the INSERT directly in the backend, rather than operating on linked tables in the frontend, as Access will be dragging vast amounts of data across the network and then sending it back. As a basic guide, something like this:
Sub sUpdateQuery()
Dim objAccess As New Access.Application
objAccess.OpenCurrentDatabase "J:\downloads\test.accdb"
objAccess.DoCmd.RunSQL "UPDATE test2 SET Field1=UCASE(Field1);"
objAccess.CloseCurrentDatabase
Set objAccess = Nothing
End Sub
Regards,
Applecore, Firstly, let me thank you for your insights. Unfortunately due to the nature of the way the data is processed, some of them I'm not sure I can implement. I have used debug.print statements to get a better idea of the timing.
You are correct, the INSERT statement is causing me the most problems, and only the second one. The deletes fly through almost instantly, no issues there. It's the second insert from the event data that is slowing it down.
I have been thinking about this since inception of how to nromalize more efficiently and create better relationships, but I'm stymied. My issue is, the data between the event table and the event table are related "in the world" but in no clear way in terms of data. There is no way to determine the relationship without a complex calculation. For example, the unique part of the application data is the application code. They are always unique. A single Application coordinator can have dozens of codes assigned to them, as can custodians, L3,L4, etc. Each event is related to an application, however, there is no specific field that is exported that tells the application code, it is obtained by parsing the event name (And yes, that is as archaic as it sounds). The event naming standards are standard mainframe 8 character names: .
For example PGRD1234 - Production job, GRD application, 1234 as the designator. So to determine what application the job is related to, I take the application code, and select LIKE with wildcards. It's not 100% accurate I am well aware, but to use wildcards, I seem to be stuck using LIKE. I haven't been able to make '=' work with wildcards. Can you?
You also mentioned "When you are inserting data into [tbl-EVENTREPORT], you are doing it when looping through a recordset - it may be faster to use an INSERT SQL statement." which I'm not sure what you are saying.. My apologies. I don't think I am understanding it. I think that is what I am doing now. I use the IIPM table to obtain the listing of the application codes I need to pull, then loop through that recordset to pull all of the event data for only those applications. As there is no direct correlation between the data, I Can't think of another way to do it.
I have a listbox that is multi-select and essentially builds a query. The syntax works as it should, but instead of a line by line entry for each sale how could I check if the array contains the field sale and if it does alter the text to SUM(Sales) As SaleAmt?
This is my current syntax:
Dim I As Long
Dim X As Long
Dim arrValues()
If lstQueryBuild.ListIndex <> -1 Then
For I = 0 To lstQueryBuild.ListCount - 1
If lstQueryBuild.Selected(I) Then
Redim Preserve arrValues(X)
arrValues(X) = lstQueryBuild.List(I)
X = X + 1
End If
Next I
End If
CurrentDb.Exeucte "Select " & Join(arrValues, ",") & " FROM holdingtable"
Which will produce
Debug.Print produces Select name, address, phone, company, sales from holdingtable
you could try substituting your Join() function with a loop through your array.
dim sSQL$
sSQL="Select "
For x=lbound(arrValues) to ubound(arrvalues)
if arrValues(x)="sales" then
sSQL=sSQL & "sum(Sales),"
else
sSQL=sSQL & arrValues(x) & ","
end if
next x
ssQL=left(sSQL,len(ssql)-1) & " from holdingtable"
CurrentDb.Execute sSQL
Hey guys I have 2 text files that look like
Text File 1 Text File2
1,asd | 4,sfsdfsdf,sdfdsf
2,dsf | 5,werewr,errret
3,dfg | 6,rty,dfgree,werer
4,dfg | 7,sdf,werwer,asdd
5,fgh | 8,tye,werew,rtyrt
I want to check file2 to see if column1( the numbers ) match up with file1 . And then print content from file2 out to a file. In this case 4 and 5 match
Desired Result : 4,sfsdfsdf,sdfdsf
5,werewr,errret
I have
Dim arrFileLines()
Dim myarray()
Dim line
Dim eachline
Dim found
Dim tokenid
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(internal_txt, 1)
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
line=arrFileLines(i)
eachline=split(line,",")
tokenid=eachline(0)
' wscript.echo tokenid
i = i + 1
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile(disabled_txt, 1)
Do Until objFile.AtEndOfStream
Redim Preserve myarray(j)
myarray(j) = objFile.ReadLine
j = j + 1
Loop
objFile.Close
For l = lbound(arrFileLines) to ubound(arrFileLines) Step 1
if arrFileLines(l) = myarray(l) then
wscript.echo arrFileLines(l)
end if
'wscript.echo arrFileLines(l)
Next
Your problem should be solved by doing an SQL Inner Join on your txt files. This is easy with ADO. Demo:
cscript 42843704-2.vbs
--------------- .\42843704-2.vbs
Option Explicit
Const adClipString = 2 ' 00000002
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sF
For Each sF In Split(".\42843704-2.vbs .\schema.ini .\42843704-1.txt .\42843704-2.txt")
WScript.Echo "---------------", sF
WScript.Echo oFS.OpenTextFile(sF).ReadAll()
WScript.Echo
Next
Dim sDir : sDir = oFS.GetAbsolutePathName(".\")
Dim sCS : sCS = Join(Array( _
"Provider=Microsoft.Jet.OLEDB.4.0" _
, "Data Source=" & sDir _
, "Extended Properties='" & Join(Array( _
"text" _
), ";") & "'" _
), ";")
Dim oDb : Set oDb = CreateObject("ADODB.Connection")
oDb.Open sCS
'WScript.Echo oDb.ConnectionString
' From simple SELECT to INNER JOIN in 4 easy steps
'Dim sSQL : sSQL = "SELECT A.ID FROM [42843704-1.txt] AS A"
'Dim sSQL : sSQL = "SELECT A.ID, B.BB, B.CC FROM [42843704-1.txt] AS A, [42843704-2.txt] AS B"
'Dim sSQL : sSQL = "SELECT A.ID, B.BB, B.CC FROM [42843704-1.txt] AS A, [42843704-2.txt] AS B WHERE A.ID = B.I
D"
Dim sSQL : sSQL = "SELECT A.ID, B.BB, B.CC FROM [42843704-1.txt] AS A INNER JOIN [42843704-2.txt] AS B ON A.ID
= B.ID"
WScript.Echo sSQL
Dim oRS : Set oRS = oDb.Execute(sSQL)
WScript.Echo oRS.GetString(adClipString, , vbTab, vbCrLf, "nix")
oRS.Close
oDb.Close
--------------- .\schema.ini
[42843704-1.txt]
ColNameHeader=False
Format=Delimited(,)
DecimalSymbol=.
Col1=ID Integer
Col2=AA Char Width 50
[42843704-2.txt]
ColNameHeader=False
Format=Delimited(,)
DecimalSymbol=.
Col1=ID Integer
Col2=BB Char Width 50
Col3=CC Char Width 50
--------------- .\42843704-1.txt
1,asd
2,dsf
3,dfg
4,dfg
5,fgh
--------------- .\42843704-2.txt
4,sfsdfsdf,sdfdsf
5,werewr,errret
6,rty,dfgree,werer
7,sdf,werwer,asdd
SELECT A.ID, B.BB, B.CC FROM [42843704-1.txt] AS A INNER JOIN [42843704-2.txt] AS B ON A.ID = B.ID
4 sfsdfsdf sdfdsf
5 werewr errret
To see that this strategy really 'works' and scales well, compare the above to SameMethodForOtherProblem
P.S. Use a 32 Bit console on a 64 Bit windows machine to be able to access the driver.
P.P.S. To get a 64 Bit driver, follow the crumbs from a to b, install the 64 bit driver, and change 1 line of the demo:
"Provider=Microsoft.Jet.OLEDB.4.0" _
to
"Provider=Microsoft.ACE.OLEDB.12.0" _
I have a few questions/changes about your approach.
First - if you don't need the text, and only the number, I would suggest using a List( of Number ) (check syntax, my VB is rusty) rather than an array. The List.Add() will come in handy and essentially do the resizing for you. Also - don't waste the memory to store what comes after the comma, since it is unnecessary.
Secondly - there is no reason to store the second file AT ALL. That can be read through, and use List.Contains(value pulled from line read in second file) line by line to determine output.
Finally, The point above is key - for the way you have structured it, the check at the end is wrong. You are only checking matching elements, and not scanning the original list.
Meaning:
if arrFileLines(l) = myarray(l) then
will only compare the first line to the first line,the second to the second, etc.
I wrote an hta app that takes one search parameter.
With this search parameter, users can search by firstname, or lastname or both.
This works really well.
Today, management decided to add date range as part of the search.
I have tried to build the WHERE clause where a user can search with lastname, first name or both OR date range but not both.
In other words, on the one hand, users can search by lastname, first name or both using a form variable called txtsrch.
Or they can just use date range only using fromMDY as fromDate and toMDY as toDate.
So far, it isn't working out well.
Whether I enter a search into the name search box or I select a date range, I get type mismatch error.
I wasn't getting type mismatch error when it was just a searchbox only.
Any assistance is greatly appreciated.
This is minimal, I think relevant code.
Date is in the form of 1/1/2013
'*
Const cOPT = "<option value='?'>?</option>"
'*
Dim fromMDY(2)
Dim toMDY(2)
Dim optMDY(2)
optMDY(0) = "<option value='0'></option>"
optMDY(1) = "<option value='0'></option>"
optMDY(2) = "<option value='0'></option>"
Dim i
'*
For i = 1 To 12
optMDY(0) = optMDY(0) & vbCrLf & Replace(cOPT,"?",i)
Next
For i = 1 To 31
optMDY(1) = optMDY(1) & vbCrLf & Replace(cOPT,"?",i)
Next
For i = Year(Date)+1 To Year(Date)-4 Step -1
optMDY(2) = optMDY(2) & vbCrLf & Replace(cOPT,"?",i)
Next
Sub Selected(What)
Select Case What
Case "FromMonth"
fromMDY(0) = FromMonth.Value
Case "FromDay"
fromMDY(1) = FromDay.Value
Case "FromYear"
fromMDY(2) = FromYear.Value
Case "ToMonth"
toMDY(0) = ToMonth.Value
Case "ToDay"
toMDY(1) = ToDay.Value
Case "ToYear"
toMDY(2) = ToYear.Value
End Select
End Sub
Sub DisplayDates()
MsgBox "From:" & vbTab & Join(fromMDY,"/") & vbCrlf _
& "To:" & vbTab & Join(toMDY,"/")
End Sub
' first: Do we use AND or OR between clauses in the WHERE?
' AndOr = ANDOR.value
Sub radiocheck()
for each b in ANDOR
if b.checked Then AndOr = b.Value
next
End Sub
' and now build up the WHERE:
where = ""
tsrch = txtsrch.Value
If tsrch <> "" Then
where = where & " Name = '" & Replace(tsrch,"'","''") & "'"
End If
If fromMDY <> "" AND toMDY<> "" Then
where = where & " convert(datetime, (left(dispdt,2) + '/' + substring(dispdt,3,2) + '/' + case when cast(right(dispdt,2) as int) >= 70 then '19' else '20' end + right(dispdt,2)), 101) Between '"& fromMDY &"' AND '"& toMDY &"' "
End If
'Take care of sql injection tactics
SQL_query = "SELECT TOP 1000 Name, Rel, Estno, dtfild, pub, [TYPE OF DOCUMENT] typeofdocument, btyp, bkno, disp, dispdt, PGNO FROM PCS60418_MTHLY_XREF WHERE " _
& where
msgBox sql_query
VBScript is Case Insensitive. ANDOR is exactly the same as AndOr so if you are doing this:
Sub radiocheck()
for each b in ANDOR
if b.checked Then AndOr = b.Value
next
End Sub
You are assigning a primitive to AndOr which conflicts with the ANDOR that is of type Array or Iteratable Object. Just rename one of the two variables and give it another try.
To prevent this kind of errors in the future: use Option Explicit, use correct (consistent) variable naming and use correct scoping: as locally as possible and passing variables to subroutines instead of using them as global variables.
I'm using VB 2010 Express to access a database in .mdb format.
I want to find the row that contains a specific string of characters and numbers, so I wrote this while loop. I'm using If statement to make sure the loop doesn't exceed the maximum rows ( MaxRows ) and throw an error. The result it keeps giving : " No record found " even though string does exist in the database. What am I doing wrong?
notes: inc is used to increment the row. message box is only to view the variables values
Code:
Dim lpn As String
Dim lpn2 As String
inc = 0
lpn = TextBox5.Text
lpn2 = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(7)
MsgBox(lpn & " " & lpn2 & " ")
While lpn <> lpn2
If inc <> MaxRows - 1 Then
inc = inc + 1
lpn2 = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(7)
MsgBox(lpn & " " & lpn2)
Else
MsgBox("No record found.")
Exit While
End If
End While
TextBox1.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(1)
TextBox2.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(2)
TextBox3.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(3)
TextBox4.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(4)
ComboBox1.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(5)
ComboBox2.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(6)
TextBox5.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(7)