Good day
ms-access 2007
I have 2 sub-Datasheet's on my form,
One display a list of items.
the other is blank.
Is there a way by placing a button on the form "Copy/Add", it copies the highlighted field to a field in the blank data-sheet.
both field's are of the same type.
Thank you
Probably the easiest thing is to run an append query. Let us say the button is on the main form, you would need a little code on the lines of:
Set db = CurrentDB
strSQL = "INSERT INTO TableB (ID, SomeField) " _
& "SELECT ID, SomeField FROM TableA WHERE ID = " _
& Me.[NameOfSubformControl].Form.[NumericIDField]
db.Execute strSQL, dbFailOnError
You will need quotes if the ID field is not numeric.
Related
I made a form in access with 2 different comboboxes. The user of
This tool can choose in combobox1: the table (which has to be filtered) and the second combobox2 is the criteria to be filtered( for example Language= “EN”) and the output of this query has to be located in tablex.
The problen what I have is that i cant find a solution for passing the value of the combobox1 to the sql statement. The second one is just like: where [language] = forms!form!combo2.value, but the part where i cant find a solution for is: select * from (combobox1 value)? How can i pass the combobox value as table name to be filtered? Can anyone please help me?
You can't have the table name in the WHERE clause of your query (there might be a hacky way to do it, but it should be discouraged at any case).
If you want to select data from 1 of a number of tables, your best bet is to generate SQL dynamically using VBA. One way to do this (especially if you want/need your query to open in Datasheet View for the end user) is to have a "dummy" query whose SQL you can populate using the form selections.
For example, let's say we have 2 tables: tTable1 and tTable2. Both of these tables have a single column named Language. You want the user to select data from either the first or second table, with an optional filter.
Create a form with 2 combo boxes: One for the tables, and one with the criteria selections. It sounds like you've already done this step.
Have a button on this form that opens the query. The code for this button's press event should look something like this:
Private Sub cmdRunQuery_Click()
Dim sSQL As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
If Not IsNull(Me.cboTableName.Value) Then
sSQL = "SELECT * FROM " & Me.cboTableName.Value
If Not IsNull(Me.cboFilter.Value) Then
sSQL = sSQL & vbNewLine & _
"WHERE Language=""" & Me.cboFilter & """"
End If
Set db = CurrentDb
Set qdf = db.QueryDefs("qDummyQuery")
qdf.SQL = sSQL
DoCmd.OpenQuery qdf.Name
End If
End Sub
Note how the SQL is being generated. Of course, using this method, you need to protect yourself from SQL injection: You should only allow predefined values in the combo box. But this serves as a proof of concept.
If you don't need to show the query results, you don't need to use the dummy query: You could just open a recordset based on the SQL and process that.
If you run the code in the afterupdate event of the combobox you can set an SQL statement like this:
Private Sub combobox2_AfterUpdate()
someGlobalVar = "Select * FROM " & me.combobox1.value & " WHERE language = " & _
me.combobox2.value
End Sub
And then call the global with the SQL string wherever you need it.
I have a query within access that selects all the contacts for a particular company based on the CompanyID Field. And on my form i have a selection of labels of which will be populated with the query result. However i'm a little stuck on how i should populate the labels, as there will be more than one contact returned from the query..
The Query
ConactData = "SELECT * FROM Contacts WHERE CompanyID = " & CompanyValue & ";"
Obviously i can do
Set rst = CurrentDb.OpenRecordset(ContactData, dbOpenSnapshot)
Me.lblTitle.Caption = rst!Title
Me.lblFirstName.Caption = rst!FirstName
Me.lblLastName.Caption = rst!LastName
Me.lblEmail.Caption = rst!Email
Me.lblMobileNumber.Caption = rst!MobileNumber
But this will just select the first result from the table, how then, can i move onto the next result? If i'm right in thinking the MoveNext method will simply go to the next record in the table, not the query result?
Why use labels? Just build the form bound to the table.
Then in your code go:
Me.RecordSource = "SELECT * FROM Contacts WHERE CompanyID = " & CompanyValue & ";"
This means you don’t need a bunch code to fill out the form, it is done for you. And your example would not allow editing of data either. To write a bunch of code when all the display of data is automatic is a waste of developer time and resources.
In fact, why not leave the form bound to the table, and then use a where clause to open the form
eg:
docmd.openform "frmContacts",,,"CompanyID = " & CompanyValue
So it not clear why you writing all that code and doing handstands - it simply not required.
I have a table 'Contacts' which has a multivalued field 'Department'. I have another table 'Outreach' and it has a field 'partner org'. I have a query which combines 'Contacts.Department' and 'outreach.[partner org]' into one field joining the two tables using an outer join on fullname field which is common in both tables.
The SQL statement I have to combine the two fields is
Contacts.Department & ";" & Outreach.[Partner Org] AS [Dept/Partner Org]
If I run this query, I get the error saying
The multivalued field 'Contacts.Department' is not valid in the expression 'Contacts.Department & " " & Outreach.[Partner Org] AS [Dept/Partner Org]'
If I add the '.Value' to the multivalued field, I get multiple rows.
Contacts.Department.Value & " " & Outreach.[Partner Org] AS [Dept/Partner Org]
I want the output to have the multivalued valued field contents followed by a ';' and the partner org name all in the same cell.
Please can someone tell me to how to get this.
Thank You.
I've come up against a similar problem with these useful (from the end-users perspective) but irritating (from the perspective of those analysing) fields.
I came up with a workaround, using a form, that I think solves your problem. I added a pseudo-departments text field to the table, applied slightly modified code from the fourth post to the relevant field(s) "AfterUpdate" events as & when they change (I pass the current record & the SQL string only summarises that record).
Dim db As Database, _
rst As Recordset, _
strSQL As String
Me.Refresh
Set db = CurrentDb
strSQL = "Select PseudoDepartment from YOURTABLE where UNIQUEIDNO = " & Me.UNIQUEIDNO & ";"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
With rst
If .RecordCount > 0 Then
.MoveFirst
.Edit
!PseudoDepartment = MakeList(Me.UNIQUEIDNO)
.Update
End If
End With
To initialise the pseudo-departments field, one could loop through all records one by one to generate the contents. One can then query the data normally and get the results in the form you indicated.
HTH.
I have a form with a subform which contains upto 100 columns however i only need to see 5 of these columns at any one time. The columns I need to see are always First name and surname plus three other columns based on the choices made in the main form (columns have labels such as HT001, HT002 etc.). One column for each of the three combo boxes available. I have looked everywhere for a solution and keep finding
Me![mycontrol].columnhidden = false
which I can see how it works for one or two columns but my code would need to be rediculously long if each column needs this. In addition to the vast quantity of columns I have 12 different subforms which are choosen from using a select case procedure. I am looking for a simple solution as I am still very much learning VBA.
If you are using a datasheet, you can dynamically set the record source according to the selections made.
Me.RecordSource = "SELECT HT001, HT002 FROM MyTable"
You can refer to the combo box when building your sql:
Me.RecordSource = "SELECT HT001, " & Me.Combo1 & " FROM MyTable"
Some notes on using a listbox with a field list from a table.
You can set the row source type to Field list and the row source to the name of a table:
RowSource: Table1
RowSourceType: Field List
In order to select multiple fields, it is important to set the multiselect property:
MultiSelect : Simple
You can iterate through the selected items in code and build a field list for use in your sql:
For Each itm In Me.List0.ItemsSelected
strSelect = strSelect & "," & Me.List0.Column(0, itm)
Next
strSelect = Mid(strSelect, 2)
sSQL = "SELECT " & strSelect & " FROM Table1"
can someone please help me with the following query
i need to update a datasheet (table) in access through a form:
i will have something like this
SQLtext = "update table1 set column1="sometext" where column2=textbox1.value"
DoCmd.RunSQL SQLtext
is this possible to do?
i have a textbox on a form and when i click a button on that form i want to update data in the datasheet where one of the columns is equal to the value property of a textbox
thank you!
This is what you need (note the subtle change)
SQLtext = "update table1 set column1='sometext' where column2='" & textbox1.value & "'"
DoCmd.RunSQL SQLtext
Note: For production code you will want to escape out any single quotes in the textbox1.value string using the string replace function, otherwise you will get a SQL error whenever a user types a single quote in that field.