Dynamically order columns MS Access subform - vba

I need to re-order columns in MS Access form on click of the button.
If Column Sequence1 button clicked order should be Col1, Col2, Col3
If Column Sequence2 button clicked order should be Col3, Col2, Col1
If Column Sequence3 button clicked order should be Col2, Col1, Col3
Please refer below screenshot for the reference
I tried, but column order is changing only when it is saved manually in the mainform or Subform. Please help on VBA script

Assuming two columns (instead of your two, but easy to extend); you can change these properties:
Private Sub Command1_Click()
With Me.MyTbl_subform.Controls
!Col1_Label.Caption = "ColA" ' Column Heading on the form
!Col2_Label.Caption = "ColB"
!Col1.ControlSource = "ColA" ' Column name from Table/Query in RecordSource
!Col2.ControlSource = "ColB"
End With
End Sub
Private Sub Command2_Click()
With Me.MyTbl_subform.Controls
!Col1_Label.Caption = "ColB"
!Col2_Label.Caption = "ColA"
!Col1.ControlSource = "ColB"
!Col2.ControlSource = "ColA"
End With
End Sub
Col1_Label, Col2_Label, Col1, and Col2 are the control names on the subform; ColA and ColB are the column names on the subform's source table/query.

Related

How do you filter records from a union query using values from a combo box list?

I have a datasheet form frmDatasheet linked to a Query named qryAll. The datasheet form is a subform of frmMain form. In frmMain, I have a combo box named filterCombo.
The filterCombo is suposed to make frmDatasheet display all records whos entryDate field is January, February and March only.
I have used this method before with a table and it worked fine but doesn't seems to work on a union query.
My code is as follows:
Private Sub filterCombo_AfterUpdate()
Dim strSQL As String
strSQL = "SELECT * FROM qryAll WHERE MONTH(entryDate) = 1 OR MONTH(entryDate) = 2 OR MONTH(entryDate) = 3"
'Task2 = "SELECT * FROM qryAll WHERE MONTH(entryDate) = 1 OR 2 OR 3"
If Me.filterCombo.Value = "First quarter(Q1)" Then
Me.frmDatasheet.Form.RecordSource = strSQL
Exit Sub
End If
End Sub
There is nothing wrong with your SQL syntax so explore other issues, such as misspelling.
Consider a simpler expression:
WHERE Month(entryDate) <= 3

Why is my WHERE clause not working in my combo box

I have a combo box called NameFilt. I wish the row source to be as follows:
SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl
WHERE (((VoucherListTbl.BuyerName) Is Not Null)) OR (((VoucherListTbl.BuyerName)<>""));
i.e show all the unique BuyerNames from my table and dont include any blanks
The above SQL is generated in the query bulider by clicking on the 3 dots in combo box's row source in the property sheet, then selecting the BuyerName field and then entering Is Not Null Or <>" in the criteria. Clicking run in the query builder displays the exact result I expect.
On closing and saving the query builder and then clicking in the combo box on the form I get a different result - All the DISTINCT names are there, but there is a blank at the start of the list.
When I attempt to use this SQL in my VBA code I get another result. The code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl WHERE (((VoucherListTbl.BuyerName) Is Not Null))
OR (((VoucherListTbl.BuyerName)<>""));"
Me.NameFilt.Dropdown
End Sub
This results in the combo box's dropdown showing only one option - a blank! There are no names listed.
Moreover, If the WHERE clause is removed i.e. the code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName FROM VoucherListTbl;"
Me.NameFilt.Dropdown
End Sub
Then the DISTINCT names are shown, with a blank option at the top of the list which is what one would expect
Please could you help by explaining why the WHERE clause will not work for me when entered into the VBA code
Many thanks
If you use "" within "" it will break the string and that's the reason of non-working WHERE condition. Encode quotes with "" i.e. end of your original string should look like <>""""));" or replace "" with '' and try again.
Note, same query can be written as
SELECT DISTINCT BuyerName
FROM VoucherListTbl
WHERE IsNull(BuyerName,'')<>''

How to add BOTH static values and records from a table as list items to a combo box in VBA

I have a combo box that gets its list items from a query. But in addition to these items, I need one static option that will be there in all cases.
I tried doing this in the naive way:
Private Sub Form_Open(Cancel As Integer)
Set rst = CurrentDb.OpenRecordset("SELECT ESDNodes.Description, ESDNodes.Sort FROM ESDNodes WHERE (((ESDNodes.parentID) =" & parentID & ")) ORDER BY ESDNodes.Sort")
Set nextSiblingSelect.Recordset = rst
nextSiblingSelect.AddItem Item:="Make Last", Index:=0
End Sub
but end up with this run-time error
The RowSourceType property must be set to 'Value List' to use this method.
which I half-expected.
Is there any trick around this? I suppose I could add a dummy record to my table set and change the query but that would be rather ugly and I would not prefer it.
Try a UNION query as the combobox RowSource. Maybe like:
SELECT Description, Sort FROM ESDNodes
UNION SELECT "Make Last", 0 FROM ESDNodes ORDER BY Sort;

condition sql statement with excel cell reference

I am applying some SQL query into a spreadsheet, and have trouble to minimize number of worksheets/connections.
i have 3 scenarios, for each of them, I use same query
select *
from ***scenario_table***
where mycolumn > 100
Now I have to do this three times for different "scenario_table", and I really wanna to use a cell as reference (let's say cell $A$1)
I wanna sth like this
select *
from
case when [$A$1] = 1 ***scenario1_table***
case when [$A$1] = 2 ***scenario2_table***
case when [$A$1] = 3 ***scenario3_table***
where mycolumn > 100
my I know whether there is any way to work it out?
Thanks
This solution uses vba.
Assuming your cell with content is in Sheet1!A1 and there is only one conecction in your workbook, insert following code in Sheet1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1")) Then
Select Case Me.Range("A1").Value
Case 1: SetTable ("Scenary1_Table")
Case 2: SetTable ("Scenary2_Table")
Case 3: SetTable ("Scenary3_Table")
End Select
End If
End Sub
Sub SetTable(ByVal TableName As String)
With ThisWorkbook.Connections(1)
.ODBCConnection.CommandText = "SELECT * FROM " & TableName
.Refresh
End With
End Sub

Checking for specify words in a grid view in vb

I want to check if a set of specific words contained in a gridview.
For example i want to check if any rows in the gridview contains the words "apple".
If it contains "apple" i want to know which are the rows that contains the word "apple".
Is that possible?
How can i go about doing it?
you can do it in RowDataBound if you used templatefield and have controls other than Label,Literal and Textbox then this may not work
i am assuming that you are using Boundfields since you have not specified any Gridview markup in the Question
Public Sub yourGridview_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For i = 1 To e.Row.Cells.Count
If e.Row.Cells(i).Text.Contains("specifiedtext") Then
'do your operations here
End If
Next
End If
End Sub
You should not search the GridView for any occurences of the given word but it's DataSource. Note that this can be different for example in case of Paging.
So i would recommend to do it in your DAL's query, for example:
SELECT IdColumn FROM Table t
WHERE Col1 LIKE '%#word%'
OR Col2 LIKE '%#word%'
OR Col3 LIKE '%#word%'
OR COl4 LIKE '%#word%'
Then you can use RowDataBound of the GridView to change the CSS(f.e. SearchMatch) of the row which ID is contained in this list.