This question already has answers here:
How to execute a query in ms-access in VBA code?
(2 answers)
Closed 8 years ago.
I have been working for MS Access payroll.. How can you call a query that's already made in Access inside a VBA function?
Public Function InsertAndUpdate()
//i want my queries here for conditioning
End Function
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("Your_Query_Name")
Related
This question already has an answer here:
Set Recordset Type of QueryDef via VBA
(1 answer)
Closed 11 months ago.
I have a MS-Access database, which includes several queries, which have the recordset type Dynaset. I want to change all Queries to the Recordset Type Snapshot (i.e. dbOpenSnapshot).
Following approach gives me the error message that dbOpenSnapshot is not a valid argument, which I took from here
Sub Properties()
Dim dbs As DAO.Database
Dim rsQuery As DAO.RecordSet
Set dbs = CurrentDb
Set rsQuery = dbs.OpenRecordset("Query1")
rsQuery.Properties("Type") = dbOpenSnapshot
End Sub
Thank you for any hints!
This is the syntax:
Set rsQuery = dbs.OpenRecordset("Query1", dbOpenSnapshot, dbReadOnly)
Database.OpenRecordset
This question already has answers here:
How do I use parameters in VBA in the different contexts in Microsoft Access?
(2 answers)
Closed 2 years ago.
Sorry for my dumb question.
I programming in Access (VBA) and I’m trying to send a variable called machine into a sql code and then insert it into a table. But i have no idea how to do it. this is my code for so far:
Private Sub MachineToevoegen_Click()
Dim SQL As String
Dim Machine As String
Machine = Machine_keuze
SQL = "INSERT INTO Machines ([Machine]) VALUES(Machine)"
DoCmd.RunSQL SQL
End Sub
If someone could help me with this it would be great.
Create a query where you pass the Machine as parameter.
For example:
PARAMETERS [prmMachine] Text (255);
INSERT INTO Machines ([Machine])
SELECT [prmMachine] AS _Machine;
Then, call the query in VBA:
With CurrentDb().QueryDefs("YourQueryName")
.Parameters("[prmMachine]").Value = Machine_keuze
.Execute dbFailOnError
End With
Try below.
Private Sub MachineToevoegen_Click()
Dim SQL As String
Dim Machine As String
Machine = "Machine_keuze"
SQL = "INSERT INTO Machines ([Machine]) VALUES('" & Machine & "')"
DoCmd.RunSQL SQL
End Sub
Im using Access 2013 and Excel 2013. In terms of References, I am using Microsoft Office 15.0 Access database engine Object Library.
So I am trying to run a DELETE query in VBA. Here is what I have so far
Sub UpdatePartList()
Dim ws As DAO.Workspace
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Dim rsSql As DAO.Recordset
Set ws = DBEngine.Workspaces(0)
Set dbs = ws.OpenDatabase("P:\Distribution Purchasing\Kit Bids\Kit Parts Query.accdb")
'deletes previous part numbers
Sql = "DELETE * FROM [Kit Parts]"
Set rsSql = dbs.OpenRecordset(Sql, dbOpenDynaset)
When I run a SELECT query for the same table, it works just fine. But when I try to DELETE, I get this error.
" Run-time error '3219': Invalid operation. "
DAO's OpenRecordSet is not appropriate for Delete queries, which do not return any recordset object. Use the Execute method instead:
Change
Set rsSql = dbs.OpenRecordset(Sql, dbOpenDynaset)
to
dbs.Execute(Sql)
also there's no need for * in the Delete SQL statement. Although Access will accept it, other systems probably won't.
You can modify your SQL as #Rahul suggested, plus change your last line of code to:
dbs.Execute sql
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a table with multiple fields in MS access. These fields have field names representing periods of time, such as 2011_2, 2011_3,..., 2013_9, 2013_10, ... , 2015_1, 2015_2...
How do I run a query that selects the next 12 months as the needed fields based on today's date? i.e, if today is 22 Dec 2013, it selects the fields : 2013_12, 2014_1, 2014_2, ..., 2014_12?
Thanks a bunch.
The short answer to your question is that you can't have dynamic field names in the Access query builder. Ideally, you should modify your data so that you have one date field. That way you can specify a date range for that field in the query criteria that doesn't require the gymnastics below.
If that's not possible, though, the long answer is that you'll need to use VBA code to construct your query. The code below should create a new query in your database (cleverly called "YourQuery") that will contain the fields you need.
Since I don't know what your table is called, it'll be "table" in this example. You should change it to whatever your table is if you use this code.
You'll need to install this code in a VBA module in your database, and then devise some means of calling the function to produce the new query. For example, you could create a macro using the "RunCode" action that uses "DynamicQuery()" as the procedure to run. Note that the function call will fail if the query for this month already exists so delete that manually if you need to run it again for some reason (you could also add additional code to function to accomplish this).
Again, just because you can do something doesn't mean you should. You really should redesign your table so you don't have to resort to workarounds like this.
Public Function DynamicQuery()
Dim strSQL As String
dim datFieldDate As Date
Dim strYearMonth As String
Dim strThisMonth As String
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim i As Integer
Set dbs = CurrentDb
strThisMonth = Year(Date) & "_" & Month(Date)
strSQL = "SELECT table." & strThisMonth
For i = 1 to 12
datFieldDate = DateAdd("m",i,Date)
strYearMonth = Year(datFieldDate) & "_" & Month(DatFieldDate)
strSQL = strSQL & ", table." & strYearMonth
Next i
strSQL = strSQL & " FROM table;"
Set qdf = dbs.CreateQueryDef("YourQuery_" & strThisMonth, strSQL)
Set qdf = Nothing
Set dbs = Nothing
End Function
If you have a form in Access with a button on it you could create the SQL statement in code as a string, eg:
MyString = "SELECT " & Year(Now) & "_9" & " FROM myTable....."
You can then execute it via code.
You won't be able to create a "standard" query that dynamically builds a list of fields to select.
This question already has answers here:
Code to loop through all records in MS Access
(3 answers)
Closed 9 years ago.
I have a access database. The database have a table. Table contain several fields including url field.
I have created a form to migrate ms access data to oracle.
Then I added a button to save data to the oracle. But before saving the url , some of the characters needed to be replaced by proper characters.
Private Sub Command59_Click()
accesstableDataSet = currentAccessSheet.gettable('tableToMigrate')
foreach ( record in accestableDataSet){
rowUrl = record.url
url = doencode(rowUrl)
exportToMysql(url)
}
How can i do something like above by writing to a access form button ?
I don't like DAO. I like ADO ;) even if there are some limitations in MS Access, ADO provides access to a broader variety of data sources than DAO, and exposes some features of the Jet 4.0 database engine that aren't available from DAO.
Dim rst As ADODB.Recordset
Dim sSQL AS String, sUrl AS String
sSQL = "SELECT *" & vbCr & _
"FROM TableToMigrate"
Set rst = New ADODB.Recordset
rst.Open sSQL, CurrentProject.Connection, adOpenStatic
With rst
'fill rst object
.MoveLast
.MoveFirst
'proccess through the
Do While Not rst.EOF
'get Url and decode it
sUrl = DecodeUrl(.Fields("Url"))
ExportToMySQL(sUrl)
.MoveNext
Loop
.Close
End With
I assume that you know how to write DecodeUrl and ExportToMySQL functions/procedures ;)