Conditionally remove rows from ms access query (SQL) - sql

I would like to remove rows (about 10000) from a report in ms access. I am comparing two sets of two column in a table. When I run the query I would like it to remove the rows where column 1 and 3 match and column 2 and 4 match. I have tried running a left join query and trying with vba code but can't get it to work.
Here is the data
I have tried using VBA
Private Sub Report_Open()
Dim prism_box As String
Dim prism_recs As String
Dim keepdrop_box As String
Dim keepdrop_recs As String
prism_box = CStr(Me.[tbl_KeepDrop_remainingpackets_RecId])
prism_recs = CStr(Me.[tbl_KeepDrop_remainingpackets_RecId])
keepdrop_box = CStr(Me.[Duplicate Recids_Box#])
keepdrop_recs = CStr(Me.[Duplicate Recids_RecId])
If prism_box = keepdrop_box & prism_recs = keepdrop_recs Then
End If
End Sub
I have tried using query too but won't delete them.
I would like the final result to report a table like this...

Since this is a report, you are better off creating a query that excludes what you don't want to see.
Pull in whatever fields you need then create two more fields that have iif([column1] = [column3], True, False) and iif([column2] = [column4], True, False Add into criteria False and then point your report's recordsource at that query. (Change the column1-4 into their actual names)

Related

Setting listboxes in MS access form - row by row

I have a requirement in MS Access where a table is displayed as several rows in the form. I have created one form detail record(several fields) that will repeat for each row in the Table1. Lets say I have five columns in the Table1. Based on Column3 value, I would like to have a list of value for Column4 and Column5 during form_load. I have also created a separate Table2 to establish relationship between Column3, Column4 and Column5. I have set up Event procedure to populate the values using sub function. The challenge I have is, not being able to set up different listbox 'value list' for different rows. Any tips on populationg form fields IMRecomExIns and AmendReasonExIns by processing each row in Table1 would be a great help.
Private Sub IMRecomExIns_Click()
Dim CoverType As String
Dim ListRecomm As String
Dim ListAmend As String
Dim db As DAO.Database
Dim tablevar As Recordset
Set db = CurrentDb
Set tablevar = db.OpenRecordset("Table2")
CoverType = "*" & Me.CoverTypeExIns.Value & "*"
ListRecomm = ""
ListAmend = ""
If tablevar.EOF = False And tablevar.BOF = False Then
tablevar.MoveFirst
Do Until tablevar.EOF
If tablevar!CoverType Like CoverType Then
ListRecomm = tablevar!Recommendation
ListAmend = tablevar!AmendReason
tablevar.MoveLast
End If
tablevar.MoveNext
Loop
End If
Me.IMRecomExIns.RowSourceType = "Value list"
Me.IMRecomExIns.RowSource = ListRecomm
Me.AmendReasonExIns.RowSourceType = "Value list"
Me.AmendReasonExIns.RowSource = ListAmend
End Sub
1) I have stored all the value list in a single cell. For example tablevar!Recommendation will have all the values for Me.IMRecomExIns.RowSource, which means the output is will look like "Rec1";"Rec2";"Rec3";etc... Same applies for tablevar!AmendReason "AR1";"AR2';"AR3";ETC... Understand this is not the normalized form of storing data. I want to POC to work before building a full solution with normalized tables.
2) Answered earlier.. the rowsource will be set with all the possible values at the first match, so no point in going all the way to the end of the table
3) CoverTypeExIns is a string, Table 2 have many different possibilities such as "Mortgage Income" and "Family Income", however the Recommendation and Amendreason are same for all "Income" category that comes from Table1. Thats why the wildcard search.
My problem is not with setting the RowSource for a single row, but setting up RowSource for multiple occurrence in of the same IMRecommmendation and AmendReason in MS Access screen.
Here is the design view of the form. This form is linked to MS Access table. For multiple rows the Detail record will repeat itself as many times.
An example of two rows displayed in the screen.
I'm not sure exactly what you are asking/trying to do here.
I can see at several problems with the code that you have:
You are using tablevar.MoveLast in the loop, whic would automatically take you to the end of the recordset.
Also, you are not concatenating (joining together) ListRecomm/ListAmend, you are just setting them equal to a value, so each loop that matches will overwrite any previous value.
Finally, I am not sure what you are doing with trying to find CoverTypeExIns - you are using LIKE, which would indicate that it is text, but not wrapping the value in single quotes. If it is a numeric value, then you should be using "=".
However, rather than opening a recordset, looping it and checking for a match to build up a list of values, it is better to just set the RowSource of listboxes equal to a SQL string (effectively a query).
Something like (assuming CoverType is numeric):
Private Sub IMRecomExIns_Click()
Dim strSQL As String
strSQL = "SELECT Recommendation FROM Table2 WHERE CoverType=" & Me!CoverTypeExIns
Me!AmendReasonExIns.RowSource = strSQL
End Sub
I prefer to declare a string to hold the SQL statement rather than setting the .RowSource directly, as it makes troubleshooting easier.
Regards,
Based on the new information given, below is some VBA code that opens up a recordset based on the information entered in "Cover", and then sets the .RowSource property of the two combo boxes to be the value lists. In my example, don't bother setting the .RowSourceType, as this should be done at design time:
Private Sub IMRecomExIns_Click()
Dim db As DAO.Database
Dim rsData As DAO.Recordset
Dim strSQL As String
Set db = DBEngine(0)(0)
strSQL = "SELECT Recommendation, AmendReason FROM Table2 WHERE CoverType LIKE '*" & Me!cboCover & "*';"
Set rsData = db.OpenRecordset(strSQL)
If Not (rsData.BOF And rsData.EOF) Then
Me!IMRecomExIns.RowSource = rsData!Recommendation
Me!AmendReasonExIns.RowSource = rsData!AmendReason
End If
rsData.Close
Set rsData = Nothing
Set db = Nothing
End Sub
As I have previously stated, you should really normalize the design of your database now, rather than getting in so far that it requires a major re-write.
Regards,

Using SQL Wildcards with LINQ

I have a question about using LINQ to access a Database and trying to make use of it's version of accessing the LIKE comparison operator.
I know that LINQ has .Contains(), .StartsWith(), and .EndsWith() as comparison methods. However I am wondering if there is a way to programatically imcorporate SQL Wildcards into a LINQ statement without explicitly using these query operators. Let me explain my situation.
I am writing a program that accesses a database, and part of the program is a search window which the user can use to help them find specific database data. I would like to try and incorporate SQL Wildcards into the textbox fields for these search pages.
For example if a user enters the input 17% I'd want the program to check for anything in that specific column that starts with a 17. The same is true with %17 and 17 where I'd want it to search for columns that end with, and contain the values.
Currently, this is the code I have for my search method:
Public Function Data_Search(sData As List(Of String), DB As CustomDataContext) As DataGridView
Dim gridData As New DataGridView
Dim query = From p In DB.Parts
Select p.Part_Number, p.Part_Description, p.Supplier.Supplier_Name
for i = 0 To sData.Count - 1
If Not sData(i).ToString() = "" Then
Select Case i
Case 0
Dim partNum As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Number.Contains(partNum))
Case 1
Dim description As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Description.Contains(description))
Case 2
Dim supp As String = sData(i).ToString()
query = query.Where(Function(x) x.Supplier_Name.Contains(supp))
End Select
End If
Next
gridData.DataSource = query.ToList()
Return gridData
End Function
So right now I am trying to see if there is a way for me to modify the code in a way that doesn't essentially involve me putting a substring search into each Case section to determine if I should be using StartsWith(), Contains(), or EndsWith.
Thanks for your time.
If you are using LINQ to SQL, and you are talking to Microsoft SQL Server, then you can use SQLMethods.Like to implement SQL LIKE:
query = query.Where(Function(x) SQLMethods.Like(x.Part_Number, partNum))

Compare Two Linq Queries

I have the following two queries that take information from the same table
I need to compare the two tables and find all the information that's value is different. Is there any way to do this without having to make a loop?
Dim housepress = (From press In db.PressInfo
Where press.PressName = pressname And press.CustomerID = "House"
Select press).ToList()
Dim curpress = (From press In db.PressInfo
Where press.PressName = pressname And press.CustomerID = Customername
Select press)
I tried using curpress.Except but I get an error that "Local sequence cannot be used in Linq to SQL

Need to Pass multiple results from query back into one memo cell

I have been all over the web for days searching for a way to accomplish the following, and I am praying some experts can help me figure it out! I have a client that wants to dynamically build a shipping "approval" email by pulling various text statements in, based on the results of queries. The shipping details will be in one table, and the statements will be in another table. There could be multiple statements returned for each shipment (e.g. several training statements, several general statements). Each of these statements needs to be added into a memo cell (created to hold each type of statement) which will then be pulled into the email template for that shipment. The table structures are as follows.
Shipment_Table
*ID
*Shipment Type - Query Criteria
*Material Category - Query Criteria
*Permit Required - Query Criteria
*General Statement (memo field to hold all general statements that match criteria)
*Training Statement (memo field to hold all training statements that match criteria)
*Approval Statement (memo field to hold all approval statements that match criteria)
Statement_Table
*Statement Type (e.g. General, Training, Approval)
*Shipment Type - Query Criteria
*Material Category - Query Criteria
*Permit Required - Query Criteria
I successfully have a query (titled StatementSearch) that joins the two and pulls in the correct statements. I can't figure out how to take the multiple query results and append them into the memo cells for the shipment.
The code that has come close is below. It seems to correctly run my query and return the results, but is not putting the results into the memo field for my test record (ID =1 just to test the code, it will eventually run off of a form and the statements will generate off the click of a button).
I hope this isn't too confusing!
Option Explicit
Function StatementUpdate()
Dim dbs As DAO.Database
Dim rstStatements As DAO.Recordset
Dim rstCBG As DAO.Recordset
Set dbs = CurrentDb()
Set rstStatements = dbs.OpenRecordset("StatementSearch")
Set rstCBG = dbs.OpenRecordset("select [St_General]from
[Cross_Border_Grid_Table] where [ID]= 1")
rstCBG.MoveFirst
'loop through each record in the CBG that matches select query
Do Until rstCBG.EOF
rstStatements.MoveFirst
Do Until rstStatements.EOF
rstCBG.Edit
rstCBG![St_General] = rstStatements
rstCBG.Update
rstStatements.MoveNext
Loop
rstCBG.MoveNext
Loop
rstCBG.Close
rstStatements.Close
Set rstStatements = Nothing
Set rstCBG = Nothing
Set dbs = Nothing
Debug.Print "Done"
End Function
Try this for your loops
Dim concStatement as String
rstCBG.MoveFirst
Do Until rstCBG.EOF
concStatement = ""
rstStatements.MoveFirst
Do Until rstStatements.EOF
concStatement = concStatement & vbCrLF & rstStatements(0)
rstStatements.MoveNext
Loop
rstCBG.Edit
rstCBG![St_General] = concStatement
rstCBG.Update
rstCBG.MoveNext
Loop
I assume, your "StatementSearch" is a query that finds all statements according to ID=1. If there is more than one recordsets "rstCBG", you have to requery rstStatements in each loop step.
EDIT:
Changed rstStatement to rstStatement(0).
Also rstStatements(0) could be wrong: Change index = 0 to the index of the field in your query. As stated, the query is not visible in your question, so I do not know the right index number.

does SSRS have a limit on number of fields in the dataset?

I have a data set:
select * from table1 --approximately 100 fields
join table2
on...
join table3
on...
The query works when I test it in SSMS.
However, when I try to run a report with this query as the dataset, I am getting:
I thought that perhaps I'm incorrectly capturing the names of the fields, so instead of doing select * i did select field1, field2...field100 but still getting the same result.
What am I doing wrong?
Please note that I have indeed made sure that all the field names are unique by doing a unique filter in excel.
I ran into the same problem. This was my solution.
1)First test your Reporting boundaries, try to determine the maximum amount of fields you can display.
2)Take your main report and make it a sub report.
3)Pass the remaining values through parameters as a concatenated string
4)Use your new sub-report to parse the parameters string.
Here is some VB code to help. Paste this in your report properties cod section.
'***************************************************************************************************************************
Public Function ListToString(myList As String, Delimiter As String, Optional index As Integer = 0) As String
'-----------------------------------------------------------------------------------
'Purpose:
'----This function splits a list and allows one to access the split list like a programmable array
'Description:
'----Input:
'--------myList: String containing the list created in SSRS
'--------Delimiter: what you used to seperate/ delimit each element
'--------index: the index you want you access
'----Output:
'--------ReturnString: returns Name in the format of "FirstName LastName"
'Version Control log: (Date - Name: Description)
'----xx/xx/xxxx Adrian Williams Creation of function
'-----------------------------------------------------------------------------------
Dim returnString As String = ""
Dim myArray As String()
myArray = myList.split(delimiter)
returnString = trim(myArray(index))
Return returnString
End Function
'***************************************************************************************************************************