Good day
I have a table(abc) with 2 fields (items = type TEXT) & (Check = type Yes/No)
I created a form, in a sub-form Table(abc) displays items, and a checkbox(check)
How do I code a query to save only the items checked in another table.
plz
It seems that you may want a Make Table or an Append Query:
Dim db As Database
Set db=CurrentDB
''Make table, NewTable must not exist
strSQL="SELECT Field1, Field2, CheckedField INTO NewTable "
& "FROM SomeTable WHERE CheckedField = True"
db.Execute strSQL, dbFailOnError
''Append query
strSQL="INSERT INTO ExistingTable (Field1, Field2, CheckedField) "
& "SELECT Field1, Field2, CheckedField " _
& "FROM SomeTable WHERE CheckedField = True"
db.Execute strSQL, dbFailOnError
Related
I have an Access Form that lists addresses and details related to those addresses. It is filtered manually by staff using the standard menus to come down to a list of relevant addresses.
I'd like to output those filtered results to a table (temp table) so that I can use it to create mailings.
Is this possible and if so what sort of code should I be using for a button.
TIA
MK
When users filtering records, they are changing .Filter property of form. Text of this property is equivalent of WHERE clause of SQL. So, all you need is to create INSERT ... SELECT... SQL query with the same source as record source of form and use filter text for WHERE clause. Something like this:
Dim str_filter As String
Dim str_recSource As String
str_recSource = Me.RecordSource
str_filter = Me.Filter
CurrentDb.Execute "DELETE * FROM MyExportTable"
If str_filter = "" Or Me.FilterOn = False Then
CurrentDb.Execute "INSERT INTO MyExportTable SELECT * FROM (" & Replace(str_recSource, ";", "") & ") as t"
Else
CurrentDb.Execute "INSERT INTO MyExportTable SELECT * FROM (" & Replace(str_recSource, ";", "") & ") as t" & " WHERE " & str_filter
End If
I've been trying to figure out what is wrong with my code. Can anyone please help?
Here is my code.
Dim addTrans_SQL2 As String = "INSERT INTO Table1(Field1,Field2,Field3,Field4) SELECT (Field01,Field02,Field03,Field04) FROM InventoryItems WHERE Field1 = " & c_Field1 & ""
Dim addTrans_dbcmd2 As OleDbCommand = New OleDbCommand(addTrans_SQL2, strCon)
addTrans_dbcmd2.ExecuteNonQuery()
c_Field1 has a value of 1.
I already checked the data type of each field in both tables and they are the same.
My strCon connection has no problem.
The only error it says is my INSERT INTO statement.
Can anyone please help? Thanks!
As we do not have the table structure, so we assume your InventoryItems have Field01 but not Field1
Let's focus on your select statement:
SELECT (Field01,Field02,Field03,Field04)
FROM InventoryItems
WHERE Field1 = " & c_Field1 & ""
The problem is the Field1 in Where clause, you may change it to Field01, which match the selection criteria
SELECT (Field01,Field02,Field03,Field04)
FROM InventoryItems
WHERE Field01 = " & c_Field1 & ""
Thank you beforehand for your assistance. I know enough about Access, SQL, and VBA to get myself into trouble. Here is what I want to do.
I want to create a query that starts with a certain year and then lists each year up until the current year. The problem is that I want the query to automatically update as time progresses. In other words, say the start year is 2009, I want my query to list 2009, 2010, 2011, 2012, and 2013 since we are currently in the year 2013. Next year, the list will expand to include 2014. I suspect this is possible using a query in SQL but not sure how to go about coding it properly.
I bet that there is no simple solution for this simple process. We must use VBA to perform following steps:
Create a temporary table:
CREATE Table tblTmpYears (
ID COUNTER CONSTRAINT PrimaryKey PRIMARY KEY,
Year Long
);
In VBA:
Dim strSQL
strSQL = "CREATE Table tblTmpYears (" _
& " ID COUNTER CONSTRAINT PrimaryKey PRIMARY KEY," _
& " Year Long" _
& ");"
CurrentDb.Execute strSQL, dbFailOnError
Fill the temporary table:
INSERT INTO tblTmpYears (year) VALUES (2009);
INSERT INTO tblTmpYears (year) VALUES (2010);
INSERT INTO tblTmpYears (year) VALUES (2011);
INSERT INTO tblTmpYears (year) VALUES (2012);
INSERT INTO tblTmpYears (year) VALUES (2013);
In VBA, for 5 years, valid even after 100 years after our life existence:
Dim y as long, ymin, ymax, strSQL
ymax = Year(Date)
ymin = ymax - 4
For y = ymin to ymax
strSQL = "INSERT INTO tblTmpYears (Year) VALUES (" & y & ");"
CurrentDb.Execute strSQL, dbFailOnError
Next
Create a query for listing with the temporary table:
SELECT * FROM tblStudents INNER JOIN tblTmpYears
ON tblStudents.Year=tblTmpYears.Year
ORDER BY Year;
In VBA like this:
Dim qdf, strSQL
strSQL = "SELECT * FROM tblStudents INNER JOIN tblTmpYears" _
& " ON tblStudents.Year=tblTmpYears.Year" _
& " ORDER BY Year;"
Set qdf = CurrentDB.CreateQueryDef("qrySelTemporary", strSQL)
DoCmd.OpenQuery qdf.Name
Here you will have the Query Datasheet Windows with your students's list, it's printable. Even better, you can use it as
MyReport.RecordSource = "qrySelTemporary"
in an Access Report with a beautiful presentation.
Delete the temporary table after printing, for example:
DROP TABLE tblTmpYears;
In VBA:
Dim strSQL
strSQL = "DROP TABLE tblTmpYears;"
CurrentDb.Execute strSQL, dbFailOnError
Only VBA can accomplish this... rather than a single SQL query.
How about this - a small VBA function that outputs the SQL for an appropriate UNION query, which you can then assign as the RowSource for a combo box, use as a sub-query inside another dynamically generated query, or whatever:
Function CreateYearsToCurrentSQL(From As Integer) As String
Dim I As Integer, S As String
For I = From To Year(Date)
If I <> From Then S = S + "UNION "
S = S + "SELECT " & I & " AS Year FROM MSysObjects" + vbNewLine
Next I
CreateYearsToCurrentSQL = S
End Function
The FROM MSysObjects is because Access will whinge about no FROM clause if one isn't there, and MSysObjects is bound to be an existing table in an Access context (if you prefer though, replace it with the name of any other table).
So I managed to create a Query Criteria that does what I need.
Like (Right(Year(Now()),2)-3) & "-" Or Like (Right(Year(Now()),2)-2) & "-" Or Like (Right(Year(Now()),2)-1) & "-" Or Like Right(Year(Now()),2) & "-"
Thank you everyone for your efforts.
I have a button which uses two separate queries that pull data from two tables and insert into another table:
Dim lngID As Long
Dim lngIDCallout As Long
Dim strSQL1 As String
lngID = CalloutAttendance_MultiSelect.Value
lngIDCallout = Forms![Callouts].[CalloutID].Value
strSQL1 = "INSERT INTO Members_Callouts(MemberID) SELECT MemberID FROM Members WHERE MemberID=" & lngID
strSQL2 = "INSERT INTO Members_Callouts(CalloutID) SELECT CalloutID FROM Callouts WHERE CalloutID=" & lngIDCallout
CurrentDb.Execute strSQL1
CurrentDb.Execute strSQL2
CalloutAttendance_MultiSelect.Requery
And whilst it almost does what I want it to do, it inserts the the two values as two separate new records, whereas I'd like it to insert it into ONE new record. I've had a go, but I either get syntax errors, or in the case below, I got a 3067 runtime error "Query input must contain at least one table or query"
strSQL1 = "INSERT INTO Members_Callouts(MemberID, CalloutID) SELECT
(SELECT MemberID FROM Members WHERE MemberID=" & lngID & "),
(SELECT CalloutID FROM Callouts WHERE CalloutID=" & lngIDCallout & ")"
Anyone know where I might be going wrong?
Thanks :-)
In this case you're just inserting the key values so all you need to do is
strSQL1 = _
"INSERT INTO Members_Callouts (MemberID, CalloutID) " & _
"VALUES (" & lngID & ", " & lngIDCallout & ")"
In other words, you don't need to bother with something like...
"(SELECT MemberID FROM Members WHERE MemberID=" & lngID & ")"
...since the value it returns is just lngId (assuming that the value exists in the [Members] table).
INSERT INTO
MyTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7)
SELECT
f1.col1, f2.col2, f3.col3, f3.col4, f3.col5, f4.col6, f5.col7
FROM
(SELECT Col1 FROM Func1()) AS f1
CROSS JOIN
(SELECT Col2 FROM Func2()) AS f2
CROSS JOIN
(SELECT Col3,Col4,Col5 FROM Func3()) AS f3
CROSS JOIN
(SELECT Col6 FROM Func4()) AS f4
CROSS JOIN
(SELECT Col7 FROM Func5()) AS f5
When I'm in table 1, form 1 I want to have a button that when clicked adds a record to table 2.
How can this be made possible?
You can run SQL or a query from VBA, for example:
Set db = CurrentDB
sSQL="INSERT INTO Table2 (FKID, Stuff) VALUES (" & Me.ID & ",'" & Me.Stuff & "')"
db.Execute sSQL, dbFailOnError
MsgBox "Records inserted " & db.RecordsAffected
Or if you have built a suitable query:
DoCmd.OpenQuery "QueryName"
Note that if you are adding to a table because you wish to add extra items to a combobox list, search for NotInList, you will find a number of suggestions.
One way (out of many):- in the click event of your button:
currentdb.execute "insert into table2 (col1, col2) select 'some_string', 12345;"