Using variables in Sourcedata - excel

How can i do this code works right, I need to use a variable
Act = InputBox("Today", "Now", Thisday)
Here i need to write: 10513 (sheet name).
Range("AA2").Select
This works OK
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="'10513'!R1C23:R117C23").CreatePivotTable TableDestination:="'[Control LE.xls]10513'!R4C27", _
TableName:="Tabla dinámica3", DefaultVersion:=xlPivotTableVersion10
This doesn't...
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="' Act '!R1C23:R117C23").CreatePivotTable _
TableDestination:="'[Control LE.xls]& Act '!R4C27", TableName:="Tabla dinámica3", _
DefaultVersion:=xlPivotTableVersion10

You're simply passing in the string Act as the worksheet name. To send this name (stored in a string variable, use string concatenation technique, note the differences below:
Debug.Print "' Act '"
vs.
Debug.Print "'" & Act & "'"
Applying this to your snippet of code, this should work:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="'" & Act & "'!R1C23:R117C23").CreatePivotTable _
TableDestination:="'[Control LE.xls]& Act '!R4C27", TableName:="Tabla dinámica3", _
DefaultVersion:=xlPivotTableVersion10

Related

Unable to Import CSV to Access

I am working on project where i have to make a report but due to heavy nature to raw dumps i am using access i can use access decently but i want to automate my manual task like import multiple files into table. for your refence i am not that great in VBA but i can do rnd and all stuff to get result or else last hope Stack overflow so below is my code everything is working fine but data is not imported in table but i can see row counts are proper so where is data is do not know please help and if possible can you guide me to short the coding as i am taking long road to code and as for error i am attaching pic of it how it looks.
thank you in advance !!!
My Code:-
Private Sub Command0_Click()
Dim Path As String
Dim SFC1, SFC2, SFQ3, SFQ4, SFQ5, SFQ6, SFC7, SFC8, SFC9, SFC10, SFC11, SFC12 As Variant
Dim SFG1, SFG2, SFG3, SFG4, SFG5, SFG6, SFG7, SFG8, SFG9 As Variant
Path = "C:\Users\Kunal.Khaire\Desktop\POD KPI Access\Raw\"
SFC1 = "13_Nulceus.csv"
SFC2 = "1_Mastersheet_Cisco.csv"
SFQ3 = "5_Overall_Performance_(2).csv"
SFQ4 = "6_Overall_Performance_(3).csv"
SFQ5 = "7_Overall_Performance_(4).csv"
SFQ6 = "8_Overall_Performance_(5).csv"
SFC7 = "9_OB_Calls_not_Tagged.csv"
SFC8 = "4_Quiz_Level.csv"
SFC9 = "2_Toggle_Count.csv"
SFC10 = "3_Agent_Disconnection.csv"
SFC11 = "11_Call_Not_Answered.csv"
SFC12 = "10_LB_Tagged_Dump.csv"
SFG1 = "APR Raw - Genesys.csv"
SFG2 = "Day-Wise Agent Level.csv"
SFG3 = "Overall Tagging Summary Data.csv"
SFG4 = "Genesys - CSAT by Date.csv"
SFG5 = "9_OB_Calls_not_Tagged (1).csv"
SFG6 = "2_Toggle_Count (1).csv"
SFG7 = "3_Agent_Disconnection (1).csv"
SFG8 = "11_Call_Not_Answered (1).csv"
SFG9 = "10_LB_Tagged_Dump (1).csv"
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="Nucleus", _
FileName:=(Path & SFC1), _
HasFieldNames:=True
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="Master_Cisco", _
FileName:=(Path & SFC2), _
HasFieldNames:=True
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="Quality_1", _
FileName:=(Path & SFQ3), _
HasFieldNames:=True
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="Quality_2", _
FileName:=(Path & SFQ4), _
HasFieldNames:=True
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="Quality_3", _
FileName:=(Path & SFQ5), _
HasFieldNames:=True
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="Quality_4", _
FileName:=(Path & SFQ6), _
HasFieldNames:=True
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="C_OBcallnottagged", _
FileName:=(Path & SFC7), _
HasFieldNames:=True
MsgBox "Importing of CSV are done !!!!"
End Sub

How to assign the "table name" of a query in the powerpivot datamodel

I'm using PowerPivot, and I'm creating a table in the datamodel using the next code:
Set ObjConn = ActiveWorkbook.Connections.Add2( _
Name:= "SomeConnName", _
Description:="DescSomeConn", _
ConnectionString:="OLEDB;Provider=OraOLEDB.Oracle;Data Source=XXXX;User ID=YYYY;Password=ZZZ;Persist Security Info=true", _
CommandText:="SELECT * FROM mytable", _
lCmdtype:=xlCmdSql, _
CreateModelConnection:=True, _
ImportRelationships:=False)
It works well, except it always create the table named "Query".
I already searched in Microsoft Documentation and googled this way too much and I can't directly find where or how to set the table/query name.... or how to rename it after it was created.
Help is appreciated.
EDIT
Thanks to #QHarr guide, I changed my approach to this:
ActiveWorkbook.Queries.Add Name:="QueryXY09", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = OleDb.DataSource(""provider=OraOLEDB.Oracle.1;data source=MyDWHConn;"", [Query=""SELECT * FROM mytable""])" _
& Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Connections.Add2 _
"Query - QueryXY09", "Connection to the 'QueryXY09' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=QueryXY09;Extended Properties=", _
"""QueryXY09""", 6, True, False
Which makes the goal, now I can set the name of the connection.
But this represents two problems. First data extraction is clearly slower, it seems it is using .net libraries, also, it looks like a double step.
Second, now I don't have a place to store the password, neither connection accept the user id/password for this approach. This last is the most worrisome, since the purpose of this is, of course, the automation of the extraction of data. However, I notice that, when I moved the file to a testing server, it prompted me for the user/password once, then I closed everything, open again and there was no prompt. So, where was the password saved?

How can I resolve error 1004 "Application-defined or object-defined error" with my macro?

My macro keeps failing at this point in my macro, where I'm filling in all the cells in a particular column with a formula reference the column next to it.
I have another nested IF formula in my macro that works fine. They do not rely on each other either. Any ideas?
ActiveCell.FormulaR1C1 = _
"=IF(ISNUMBER(SEARCH(""Nurture"",RC[-1])),""New Producer Nurture"",IF(ISNUMBER(SEARCH(""WB FU"",RC[-1])),""Event Follow Up"",IF(ISNUMBER(SEARCH(""WS FU"",RC[-1])),""Event Follow Up"",IF(ISNUMBER(SEARCH(""WS REM"",RC[-1])),""Event Promotion"",IF(ISNUMBER(SEARCH(""Marketing OB"",RC[-1])),""General Marketing"",IF(ISNUMBER(SEARCH(""Marketing Direct to Agent"",RC[-1])),""" & _
"Marketing"",IF(ISNUMBER(SEARCH(""Info Email Response"",RC[-1])),""General Marketing"",IF(ISNUMBER(SEARCH(""Marketing IB"",RC[-1])),""General Marketing"",IF(ISNUMBER(SEARCH(""ZProgramsMatch"",RC[-1])),""General Marketing"",IF(ISNUMBER(SEARCH(""Registration Support"",RC[-1])),""General Marketing"",IF(ISNUMBER(SEARCH(""Web Contact Form Outreach"",RC[-1])),""General Mar" & _
"IF(ISNUMBER(SEARCH(""General Product Inquiry"",RC[-1])),""General Marketing""))))))))))))"
You should try it like this....
ActiveCell.FormulaR1C1 = _
"=IF(ISNUMBER(SEARCH(""Nurture"",RC[-1])),""New Producer Nurture""," & _
"IF(ISNUMBER(SEARCH(""WB FU"",RC[-1])),""Event Follow Up""," & _
"IF(ISNUMBER(SEARCH(""WS FU"",RC[-1])),""Event Follow Up""," & _
"IF(ISNUMBER(SEARCH(""WS REM"",RC[-1])),""Event Promotion""," & _
"IF(ISNUMBER(SEARCH(""Marketing OB"",RC[-1])),""General Marketing""," & _
"IF(ISNUMBER(SEARCH(""Marketing Direct to Agent"",RC[-1])),""Marketing""," & _
"IF(ISNUMBER(SEARCH(""Info Email Response"",RC[-1])),""General Marketing""," & _
"IF(ISNUMBER(SEARCH(""Marketing IB"",RC[-1])),""General Marketing""," & _
"IF(ISNUMBER(SEARCH(""ZProgramsMatch"",RC[-1])),""General Marketing""," & _
"IF(ISNUMBER(SEARCH(""Registration Support"",RC[-1])),""General Marketing""," & _
"IF(ISNUMBER(SEARCH(""Web Contact Form Outreach"",RC[-1])),""General Marketing""," & _
"IF(ISNUMBER(SEARCH(""General Product Inquiry"",RC[-1])),""General Marketing""))))))))))))"

ms access form criteria help error

I am trying to open a form with criteria.
My criteria where I am getting the error.
strCriteria = "WorkID = 3 And
OptOut= -1 And
AppointmentDate = (Last(AppointmentDate))>DateSerial(Year(Date()),Month(Date())-3,1) And
(Last(AppointmentDate))<DateSerial(Year(Date()),Month(Date())-2,0)"
I placed it like this in order to read it easier.
My error number is 3096.
Thank you.
==== Update I give up =======
My code so far.
strSQL = "SELECT tblAppointment.WorkID," & _
"tblCustomer.OptOut," & _
"Last(tblAppointment.AppointmentDate) AS LastAppointmentDate," & _
"tblAppointment.CustomerID," & _
"tblCustomer.Surname," & _
"tblCustomer.Name," & _
"tblCustomer.FatherName," & _
"Last(tblAppointment.AppointmentMemo) AS LastAppointmentMemo" & _
"FROM tblCustomer INNER JOIN tblAppointment ON tblCustomer.CustomerID = tblAppointment.CustomerID " & _
"GROUP BY tblAppointment.WorkID," & _
"tblCustomer.OptOut," & _
"tblAppointment.CustomerID," & _
"tblCustomer.Surname," & _
"tblCustomer.Name," & _
"tblCustomer.FatherName " & _
"HAVING (((tblAppointment.WorkID) = 3) And ((tblCustomer.OptOut) = -1) And " & _
"(LastAppointmentDate > DateSerial(Year(Date()), Month(Date()) - 3, 1) And " & _
"LastAppointmentDate < DateSerial(Year(Date()), Month(Date()) - 2, 0)))" & _
"ORDER BY LastAppointmentDate, " & _
"tblCustomer.Surname," & _
"tblCustomer.Name," & _
"tblCustomer.FatherName;"
I see you are still having trouble. Let's look at the code you have given us.
Your having statement is looking for a field that does not exist on the table. You currently have:
Having (((tblAppointment.WorkID) = 3) And ((tblCustomer.OptOut) = -1) And " & _
"(LastAppointmentDate > DateSerial(Year(Date()), Month(Date()) - 3, 1) And " & _
"LastAppointmentDate < DateSerial(Year(Date()), Month(Date()) - 2, 0)))" & _
Your having clause is looking for LastAppointmentDate on your table. This field does not exist since the field name is AppointmentDate. Change your field name in your having statement to match the field name and it should work. You also have missing parenthesis.
Having (((tblAppointment.WorkID) = 3) AND ((tblCustomer.optout) = -1) AND " & _
"((tblAppointment.AppointmentDate) > DateSerial(Year(Date()), Month(Date()),-3,1)) AND " & _
"((tblAppointment.AppointmentDate) < DateSerial(Year(Date()), Month(Date()),-2,0))) " & _
Try this solution to your having statement. If it does not work, let me know and I'll do more digging.
There isn't much information to go off of with your question, but I'm going to take a stab at it.
I'm assuming you are trying to open a form with the following code:
docmd.openform "FormName",acnormal,,strcriteria
If this is the case, you variable is looking for a last appointment date that hasn't been established or discovered yet. basically, you set a criteria to a field on a form that isn't loaded yet, thus, no information can be used.
You can try a different approach that has done justice for me multiple times and I continue to use this method today.
private sub Eventtrigger()
dim frm as form
dim strSQL as string
strsql = "SELECT * " & _
"FROM TableName " & _
"WHERE (((TableName.workID) = 3 AND (TableName.OptOut) = -1 AND (TableName.AppointmentDate) > DateSerial(year(date()),Month(Date())-3,1) AND (TableName.AppointmentDate) < DateSerial(year(date()),Month(date())-2,0)));"
'Edited since I missed 2 closing parenthesis
Docmd.openform "FormName",acnormal
set frm = [forms]![FormName] 'New form opened
frm.recordsource = strsql
EndCode:
if not frm is nothing then
set frm = nothing
end if
end sub
The above code will allow you to set the recordsource of the form to the newly created query. which the query will filter the results for you.
Or, to fix you variable, Just set your variable as follows:
strcriteria = "WorkID = 3 AND OptOut = -1 AND " & _
"AppointmentDate > DateSerial(Year(date()),Month(date())-3,1) AND " & _
"AppointmentDate < DateSerial(Year(date()),Month(date())-2,0)"
If you need last, use last on AppointmentDate:
Last(AppointmentDate) > DateSerial(Year(date()),Month(Date())-3,1) AND " & _
Last(AppointmentDate) < DateSerial(Year(date()),Month(Date())-2,0)
Let me know if either of these methods/repairs didn't work and I will do more digging.

how to specify database user credentials in a connection array string (Excel VBA)

Please help in modifying the connection array string (Excel 2010) to be able to receive the input from a cell reference for fields UID=;PWD= and Initial Catalog.
.Connection= Array("OLEDB;Provider=MSDASQL.1;Persist Security Info=True;Extended Properties=""DSN=NZSQL;Database=consumerdb;Servername=192.54.97.102;", "UID=USERNAME;PWD=****;Port=5480;ReadOnly=0;SQLBitOneZero=0;LegacySQLTables=0;NumericAsChar=0;ShowSystemTables=0;LoginTimeout=0;QueryTimeout=0;DateFormat=1;SecurityLevel=preferredUnSecured;CaCertFile="";Initial Catalog=CONSUMERDB_USERNAME")
The Connection string is an array of simple strings. You need to locate the one containing the user name and password, then break that section apart and insert the values from the cell. Put sections of the connection string on different lines to help break it up. Break up each section if it helps to isolate the substring that you want to manipulate.
.Connection = Array("OLEDB;Provider=MSDASQL.1;Persist Security Info=True;" & _
"Extended Properties=""DSN=NZSQL;Database=consumerdb;Servername=192.54.97.102;", _
"UID=USERNAME;PWD=****;" & _
"Port=5480;ReadOnly=0;SQLBitOneZero=0;LegacySQLTables=0;NumericAsChar=0;" & _
"ShowSystemTables=0;LoginTimeout=0;QueryTimeout=0;DateFormat=1;" & _
"SecurityLevel=preferredUnSecured;CaCertFile="";Initial Catalog=CONSUMERDB_USERNAME" _
)
Now if is easy to find the username and password and concatenate the values into that portion.
Dim usr As String, pwd As String
usr = Range("A1").Value
pwd = Range("B1").Value
.Connection = Array("OLEDB;Provider=MSDASQL.1;Persist Security Info=True;" & _
"Extended Properties=""DSN=NZSQL;Database=consumerdb;Servername=192.54.97.102;", _
"UID=" & usr & ";PWD=" & pwd & ";" & _
"Port=5480;ReadOnly=0;SQLBitOneZero=0;LegacySQLTables=0;NumericAsChar=0;" & _
"ShowSystemTables=0;LoginTimeout=0;QueryTimeout=0;DateFormat=1;" & _
"SecurityLevel=preferredUnSecured;CaCertFile="";Initial Catalog=CONSUMERDB_USERNAME" _
)

Resources