I am working on an extant .Asp program at the moment which contains the following snippet:
Dim strRel_Report_Location
Dim rs, sql, blnReportExists
Dim strUID
Dim intMonth, intYear
sql = "Select NewID() AS UID"
Set rs = DB.Execute(Sql)
strUID = rs("UID")
strRel_Report_Location = "/Admin/Reports/Temp/t" & strUID & ".html"
My question is, how is it possible to pass the string "UID" as an argument to the object 'rs'? One may easily divine from the code that the intent is to pass the resulting "NewID()" call to the path which is assigned in the statement:
strRel_Report_Location = "/Admin/Reports/Temp/t" & strUID & ".html"
Any illumination would be greatly appreciated as I don't wish to just make assumptions on the code with which I am working.
I'm sure this is a layup for you guys, but thank you for your help!
I have to make an assumption here because you never show how the DB variable is created. I am going to assume you are working with straight ADO because the call signatures in your code match that.
Again, you are probably working with an Ado Command instance which has the method Execute. This method takes a sql string, executes it on the database server, and then returns a Recordset which contains the results of the query that executed. The recordset has an indexer with the name of the columns, using that indexer you can get the value in the column.
If you want to learn more about this code it would help to start reading up on how to work with ADO.
Related
I'm using ADO because I want to write to a Sharepoint list using VBA in Excel.
Right now I am getting "The Microsoft Access database engine could not find the object 'Isaac Test Excel To Sharepoint', and the code errs on the INSERT line. I suspect it is because of either my site reference being wrong, or my list ID being wrong.
I don't think my list ID is wrong, because I carefully followed the directions to extract the list ID from the URL that's exposed when you go to List Settings, carefully Replacing the 3 replaceable items as mentioned here: https://community.nintex.com/t5/Community-Blogs/Obtaining-a-list-id-in-SharePoint-2010-or-2013/ba-p/77664#:~:text=Navigate%20to%20the%20list%20and%20click%20List%20Settings.,Guid%20Format%20with%20URL%20encoding).
I am passing it in as:
strSharepointListID = "{3404D534–10CB–4F53–BB9D–37F5612155F1}"
I would like to have concluded, "the connection is correct because the code doesn't err until all the way to the INSERT statement", but unfortunately I've proved that to be false: If I pass in a totally non-existent Site value, gibberish, the code still doesn't err until all the way at the INSERT statement.
The name of my list is definitely Isaac Test Excel To Sharepoint
The site I am passing is like this, with me sanitizing this by replacing some text with "text": (I've tried all 3 of these):
strSharepointSite = "https://text.text.text.com"
strSharepointSite = "https://text.text.text.com/sites/text"
strSharepointSite = "https://text.text.text.com/sites/text/_layouts/15/start.aspx#/"
Full code:
Sub Upd2KPIMember_SP()
Dim cnt As ADODB.Connection
Dim mySQL As String
Dim strSharepointListID As String, strSharepointSite As String
'https://community.nintex.com/t5/Community-Blogs/Obtaining-a-list-id-in-SharePoint-2010-or-2013/ba-p/77664#:~:text=Navigate%20to%20the%20list%20and%20click%20List%20Settings.,Guid%20Format%20with%20URL%20encoding).
'list ID from sharepoint URL:
' %7B3404D534%2D10CB%2D4F53%2DBB9D%2D37F5612155F1%7D
'list ID after replacing as follows:
' %7B3404D534%2D10CB%2D4F53%2DBB9D%2D37F5612155F1}
strSharepointListID = "{3404D534–10CB–4F53–BB9D–37F5612155F1}"
strSharepointSite = "[sanitized for SO post]"
Set cnt = New ADODB.Connection
With cnt
.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=" & strSharepointSite & ";LIST=" & strSharepointListID & ";"
.Open
End With
mySQL = "insert into [Isaac Test Excel To Sharepoint] (column1,column2) values ('col1_val1','col2_val1');"
cnt.Execute (mySQL)
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
End Sub
I'm also fairly sure the SQL syntax is good, because the code DID tell me when it was wrong--When at first I used INSERT TABLE instead of INSERT INTO TABLE.
Partial credit to KeshavSharma (see comments) for
correctly mentioning LIST ID not required, use LIST NAME instead
inspiring me to keep focusing on THAT line of code - that it was the problem
The final code that worked was precisely identical to the first code I posted, EXCEPT, inside the connection string, instead of:
LIST={8F7FEF30–C868–4480–8AC8–4FE4FDB3921A};
I need to use:
LIST=Isaac Test Excel To Sharepoint;
(despite spaces in object name - I need to use NO single quotes, NO brackets).
Very happy this got solved - hopefully it helps someone else some day.
I'm trying to extract data from an Access 2016 database using SQL and VBA. Below is the code I'm trying to use and every time I run it, I get a "No value given for one or more parameters". I've also shown what I see in the immediate window.
vsql = "SELECT [ResDate],[ResNanme],[ResStart],[ResEnd] FROM [TrainingRoom] where Month([ResDate]) = " & MonNo
Set RecSet1 = Connection.Execute(vsql, dbrows, adCmdText)
Immediate Window:
SELECT [ResDate],[ResNanme],[ResStart],[ResEnd] FROM [TrainingRoom] where(Month([ResDate])) = 11
I don't see anything wrong but I'm sure this is user error. The "MonNo" variable is declared as an integer.
Any suggestions would be greatly appreciated. Thanks for the help.....
I've never used Execute method to open ADO recordset, only Open.
Assuming Connection is a declared and set variable - but it is a reserved word and really should not use as a variable. If code is behind same db pulling data from, could just use CurrentDb with DAO recordset variable and OpenRecordset method. Example that accepts default parameters for optional arguments:
Dim RecSet1 As DAO.Recordset
Set RecSet1 = CurrentDb.OpenRecordset(vsql)
When I use this SQL in Access, it works fine. In VBA, the result is to create a new table with no records. It must be a dumb mistake on my part but can't find it. Many thanks for any help or point in right direction.
Mike
strSQL = "SELECT tempconsoltb.[sub-accountNumber],tempconsoltb.[sub-accountDescription], tempconsoltb.sumOfCMBaseCurr INTO tblTempConsolTB FROM tempConsolTB;"
Set qdf = CurrentDb.CreateQueryDef("", strSQL)
qdf.Parameters(0) = Forms!frmMain.txtPM
qdf.Parameters(1) = Forms!frmMain.txtPY
qdf.Parameters(2) = Forms!frmMain.cboBaseFX
qdf.Parameters(3) = Forms!frmMain.cboConsolCo
qdf.Execute
Try checking that the objects in your forms are named properly and are actually feeding the values into the query. You can always use the Watch function in your compiler to see the values there or create a string variable that you initialize its value within the code to see what your actual query and put a watch on it when running the code.
edit still recieving type mismatch error but updated code with help from comments
-updated code shown below
I am new to ASP and VBScript. I am trying to insert post data into a SQL database, however, I want to ensure the query is sterilized so I am trying to use a parameterized query. When I execute the query I get this error.
Microsoft VBScript runtime error '800a000d'
Type mismatch
my code looks like this
Set conn = Server.CreateObject("ADODB.Connection")
conn.Mode = 3
conn.open "Provider=SQLOLEDB;Data Source=xxx.xxx.xxx.xxx,xxxx;
database=db_example;uid=user;pwd=password;"
Dim oCmd
set oCmd= Server.CreateObject("ADODB.Command")
Dim sSQL
sSQL = "INSERT INTO tbl_Application (Expenses, Name, Why) VALUES (?, ?, ?);"
oCmd.CommandText = sSQL
oCmd.ActiveConnection= conn
Dim uPar
uPar = oCmd.CreateParameter("Expenses",200,1,255,session("Expenses"))
oCmd.Parameters.Append(uPar)
Dim vPar
vPar = oCmd.CreateParameter("Name",200,1,255,session("Name"))
oCmd.Parameters.Append(vPar)
Dim wPar
wPar = oCmd.CreateParameter("Why",200,1,255,session("Why"))
oCmd.Parameters.Append(wPar)
Dim oRS
oRS = oCmd.Execute()
I have tried typing the data as a varchar and a char. Please let me know if you have a fix for my problem. Also, I am intending to insert more than three pieces of data on the site - is there a better way than going through and making a parameter manually for each column?
The documentation for the CreateParameter method state that:
If you specify a variable-length data type in the Type argument, you
must either pass a Size argument or set the Size property of the
Parameter object before appending it to the Parameters collection;
otherwise, an error occurs.
129 corresponds to adChar, which is a variable-length data type and therefore requires you to pass a Size argument. Usually, you should use the defined length of the column in the database, but I have found that just using the length of the value I'm passing works also.
I have been looking around but can't seem to find out how to do this.
I'm trying to execute a SELECT sql statement in VB that takes in a parameter from a form. For example the user selects a From and a To date and the SQL should take what they selected.
I know that you cannot use the DoCmd.Execute method for SELECT SQL so I would need to open the record set - but how? This is what I have tried so far
Dim recordSet As DAO.recordSet
Dim SQL As String
SQL = "SELECT * FROM tblWebMeetingData"
Set recordSet = CurrentDb.OpenRecordset(SQL)
'More code in here
recordSet.Close
Set recordSet = Nothing
Because this executes without an error I assume it's actually getting the results - so is there a way to see what it is actually returning?
Thanks
First: It's a good advice to rename the recordset to rs, for example, because "recordset" is a reserved name. This is misunderstandable.
This recordset contains the records you queried by your SQL statement. You may access those data by rs.fields("fieldname").value. Move to the next recordset with rs.movenext.
To incorporate the form's control value I use the way to build the full SQL statement prior to opening the recordset. Say the form is named "myform" and the control "mycontrol", you may write some kind of
SQL = "SELECT * FROM tblWebMeetingData WHERE myfield = " & forms!myform.mycontrol.value
Please be sure the form only contains valid values, because any wrong formatted value will directly lead to an SQL execution error.
I hope it was this, what you wanted.
Here you have come sample code about iterating trought RecordSet and using values from it( I hope it helps a bit):
Dim i As Integer
Do While Not rs.EOF
Sheets("D_" & day).Cells(i, 1) = rs.Fields(0).Value
Sheets("D_" & day).Cells(i, 2) = rs.Fields(1).Value
rs.MoveNext
i = i + 1
Loop
rs.Close