Only Show specific word from a string - ms-access

I'm currently learning Access 2016 since i find it works wonders with Excel.
However i'm trying to find the correct SQL code for detecting certain words in Long Text and only output these words not the text itself
Here is the setup
Fieldname: Type
Long Text
Cell Information
Cell1: Johan have a nice car
Cell2: Jane road a bike.
Cell3: Janes bike was red
Output
Cell1: car
Cell2: bike
Cell3: bike
I'm only interested in keeping certain words from these strings when the query is done, and they should be output to the same Fieldname, i have tried to locate this information for the most part of the day and my last hope of sanity was to check in here if someone has an idea of which function i should be using.
Thank you for your time

You will probably have to use VBA for this, depending on how big your list of words is, but one way to do it with SQL, assuming a relatively small and FIXED list of words:
IIF(<Mytext> LIKE '*car*','car',IIF(<Mytext> LIKE '*bike*','bike',''))

You could either read the values from a table or fill an array. Both would work, but I prefer table-driven code because, for something like this, adding new search terms would only require you to add them to your table.
So, what I would do is first create a new table called tblSearchTerms. In that table, create one field called SearchTerms, and put each of your terms (i.e. "Car", "Bike", etc...) in there, one per record.
Then set up a function in Access. Open a Module (either a new one or one you already have, as long as it's a Module), and enter this:
Function SearchExists(sString As String) As String
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset("Select SearchTerms from tblSearchTerms")
Do while rec.EOF = False
If InStr(1, sString, rec(0)) > 0 Then
SearchExists = "" & rec(0) & ""
Goto BugOut
Else
End If
Loop
SearchExists = "Not Found"
BugOut:
End Function
Now, what you do is create a query. In that query, you'll have one field to check to see if your search term exists. It will look something like this:
MySearchTerm: SearchExists([Cell1])
When you run your query, that field will either populate with one of the search terms (if it's found), or will return "Not Found".
This may require a bit of tweaking, because it's more for finding a search term in an Access table, so there's going to need to be some adjusting to make it work with Excel, but it's pretty close to what you need.

Related

Access writng to wrong row number

4150
NRrows = RSNonResourceCosts.RecordCount ' Number of Rows in Non Resource Table
NRCols = RSNonResourceCosts.Fields.Count ' Number of Fields in NonResource Table
Dim CL(1 To 10) As Integer ' This is to count "filled rows" when spreadsheet is filled
Dim Header(1 To 10) As String
'-----------
'Find the Headers (Taken from Actual Table and not predefined as original)
For Each Recordsetfieldx In RSNonResourceCosts.Fields
If C > 0 Then
Header(C) = Recordsetfieldx.Name
End If
C = C + 1
Next Recordsetfieldx
4170
R = 0
'Write to worksheet
RSNonResourceCosts.MoveFirst
Do Until RSNonResourceCosts.EOF
For C = 1 To NRCols - 1
FieldName = RSNonResourceCosts.Fields(C).Value
If RSNonResourceCosts.Fields(Header(C)).Value <> "" Then
CL(C) = CL(C) + 1
WKS.Cells(200 + R, C) = RSNonResourceCosts.Fields(Header(C)).Value
End If
Next C
RSNonResourceCosts.MoveNext
R = R + 1
Loop
I attach code. Have solved part of original by defining Recordset. User can add column to Table. First part of code determines the headers. Second part determines values and writes to worksheet. The new Rows are appearing first on the worksheet and in wrong column. I tried attaching worksheet but it looked awful. Any help would be appreciated.
Two things:
1) The order your records is the order they are in the recordset. If you want them in a particular order, try sorting them (perhaps with an ORDER BY in the underlying SQL statement)
2) For the column issue: In the first bit of code, I don't see where C is initialized, but keep in mind the Headers and Fields both start with an index of 0, so if you set Header(1) = the first field's header (index 0), but then copy the data in the fields without shifting the index value, it will shift everything over by one column.
As an added note, you might want to consider what happens when you have more than 10 columns. Using fixed-length arrays means your code will break. You might want to read about using a dynamic array and ReDim.
I don't yet feel like I have completely grasped the entirety of the problem yet, but let me take a stab at it. From what I do understand, data is being written from your record set into excel (good), but it is going into the 'wrong row' (question title) and the 'wrong column' (question text).
From what I see, I don't know the purpose of FieldName = RSNonResourceCosts.Fields(C).Value, but I want to make sure that you understand that RSNonResourceCosts.Fields(C).Value is not necessarily equivalent to RSNonResourceCosts.Fields(Header(C)).Value. More than that, you are likely missing at least one column altogether in your output, or at least skipping over it accidentally. rs.Fields(0).name is the first 'column' in a recordset, but it is completely ignored in your code. Perhaps this is intentional, maybe it is a key field or something useless to you, but it is important that you are making that distinction intentionally. But, since I don't see where your code populates the headers in your worksheet, I wonder if 'wrong column' means every record has been shifted a column and your last column is sitting empty. That, coupled with the dubious omission of C being initialized as 0 (not 1, or anything else) in your above code, makes me concerned that Header(3) could possibly by field(1), or field(4), or I don't know. That would certainly also confuse the columns in your output, or at least make dependence on FieldName frustrating.
Another thing, really a shot in the dark: NRrows. I have had issues before, depending on how I create my recordset, of not getting the correct record count the first time. And, if I base the population of a worksheet, array, etc., on the number of rows and the records relative position in that number, my records get all sorts of wacky. Maybe you did this already, but since it isn't shown, I recommend a RSNonResourceCosts.movelast: RSNonResourceCosts.movefirst line before you define NRrows, just to be sure.
And last, if I am way off base here... then you really are going to have to show us the spreadsheet, even if it isn't your most beautiful work. We all know that if it were, you wouldn't be asking about it here... so set your pride aside, and be more specific as well as show us what the output looks like and how it should look.

Multiple appearances of a name in a SharePoint list, but need only one for BIDS report

I'm building a report in BIDS based on a SharePoint list. I want to make a multi-valued parameter to filter the list by a name, but each name shows up multiple times in the list. The report is already going to be filtered by work site, and I need this filter in addition. I'm pretty new to SSRS, so there may be a simpler solution, but I've thought of two ways this could be done, though neither is particularly efficient.
One way is to create a separate MVP for each site, and manually write out each name for that site in the parameter's Available Values. The problem is that I don't think there's a way to hide a parameter based on which site you're looking at; for instance, if I'm looking at Site A, the drop-down lists for Sites B-Z would all also show up. They wouldn't do anything, since the superfluous sites would already be filtered out, but it'd be an eyesore. This approach would also be difficult to maintain in the long-run, requiring someone to come in and manually add every new name to the parameter.
The other, better approach is to add a new dataset with only names and site IDs, and find some way to filter out every repetition of a name, so all that's left is the name and the associated site. I have no idea how this could be done, though.
Finally found an explanation: http://blogs.msdn.com/b/sqlforum/archive/2011/04/28/walkthrough-how-to-get-distinct-values-of-a-column-of-a-sharepoint-list-using-sql-server-reporting-services.aspx
Essentially, this has you create one parameter to serve as a sorta jury-rigged dataset based on a piece of custom VB code. That first parameter is then used to populate a second, multi-valued parameter, which contains all unique values.
The VB code:
Public Shared Function RemoveDuplicates(parameter As Parameter) As String()
Dim items As Object() = parameter.Value
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As [String]() = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
NB: When you set up the dataset in this process, make sure to alter the query to reflect your own data source; don't just copy/paste blindly.

Examples of how to create text fields, size them, and put them in columns on a report

The 1,500 page Access 97 Bible (don't laugh!) that I've been given by my boss to solve his problem doesn't solve my problem of how to solve his problem, because it has nee VBA code.
Let me first make clear that I've made attempts to solve this without (much) coding, and that I've coded quite a bit in VBA already, so I'm basically familiar with most things including recordsets, queries, etc etc but have problems with MS Access limits on how to form a report with data coming from VBA variables. I'm also versatile in most programming languages, but this is not a language problem but rather a "how to/what's possible" problem.
My problem right now is that dragging the query fields into the Detail subform and putting them into cells in columns setting Left and Top with VBA code are moving them alright, but each cell is on a new page. Unfortunately, there is multiple data in each cell that won't conform to the Create Report Guide options available.
So my question is simply this: Can someone point me to working examples of code that create, place, and fill with VBA variable strings, text fields at any coordinate I please on a paper size of my choice?
Edit: The above is not an option, as I understand this will prohibit the client from getting an .mde database. What remains, then, is to merely ask for some sound advice on how to get several rows GROUPed BY weekday and machine (see below) into a recordset or similar for each cell. I guess the best way is to count the number of columns in the table (machines in the sql result) and create 5 rows of these with dummy data, then go through the result rows and place the data in the relevant controls. But if you have ideas for doing this work better and faster, write them as answers.
Sorry for this, I knew there was something I wasn't understanding. Basically, I thought Access supported creating reports dynamically via VBA, ie. "generating pages with data" rather than "preparing a flow of controls connected to datasources". But Access requires that you create an ample amount of dummy, unlinked controls manually, then either fill or hide them and that's how they become "dynamic".
This is for Access 2003 on a remote server accessing local and remote ODBC SQL database tables, if relevant. The goal is to make a week schedule of n columns (n=number of machines at a certain plant) x 5 rows (weekday Mon-Fri), and put 1 or more recordset rows (=scheduled activities for that day on that machine) in each of the "n by 5 table" cells.
If you detect venting frustration in this post I can only ask your forgiveness and hope for your understanding.
So, has many techniques for this:
Ex: 1) using dinamic sql for this:
'Create a function to make sql query
Function MakeMySQlReport(Parameters):
Dim strSql as string
Dim strMyVar as string
strsql = vbnullstring
strsql = "Select " & myVar1 & " as MyFieldVar1, * from myTable where Fieldx =" & Parameters
MyReport.recordSource = ssql
End Function
Ex: 2) create function that returns yours strings:
Function MyString1() as string
MyString1 = 'ABC'
end Function
An in your report, select the textbox will receive the value and type =MyString1()]
I hope this help to you, need more examples?
Solution:
Create many objects manually (grr!)
name them systematically
put them in a Control Array (get all Me.Controls, sift out the ones you're interested in, and put them in an indexed array)
go through the array and change their properties

MS Access: Auto update fields in same table

Sorry but I'm not very experienced when it comes to things like this.
I have a table called "Measure Table" with the fields "ID", "Proxy" and "ProxyID".
I created a form based on this table. ID is a label pre-populated from the table. Proxy is a drop down menu with the options "For" or "From". ProxyID contains a drop down with the same numbers as ID.
I would like a user to go to a specific record in the form (say for ID:I800), select "For" from the Proxy drop down and then select ProxyID (lets say L800). For the record for L800, I want it to automatically change the proxy to "From" and the ProxyID to I800.
Is this possible in Access?
Thanks so much for any help provided
Here is a visual of what i wnat to happen:
I want the table to look like this before the update(when the user selects "For" and "L800"):
Record# ID Proxy ProxyID
1 I800 For L800
2 L800
Then the table is automaticaly updated to:
Record# ID Proxy ProxyID
1 I800 For L800
2 L800 From I800
Okay, here is the gist of what you need to do to solve your immediate problem (updating the corresponding row in the other table.
Simply add an event handler to the AfterUpdate event of the form to perform the update to the other row. The code should look very similar to this...
Private Sub Form_AfterUpdate()
Dim RelatedID As String
Dim Proxy As String
If (UCase(Me.Form!Proxy) = "FOR") Then
RelatedID = Me.Form!ProxyID
CurrentID = Me.Form!ID
DoCmd.RunSQL ("UPDATE [Measure Table] SET ProxyID='" & CurrentID & "', Proxy='From' WHERE ID='" & RelatedID & "'")
End If
End Sub
Caveats:
As I mentioned in the comments, this data structure is a very bad idea and will create a lot of extra work for you to maintain the data integrity according to the implicit rules you are specifying as a matter of course with this design. I realize you have an existing DB to deal with, but frankly it would probably be less work to fix the DB design than maintain this one in the long run.
Some additional considerations you didn't ask about, but are going to need to deal with:
What happens if someone updates
either of the entries in a pair
directly in the table instead of
using your form? There really isn't a
good way to apply the above logic to run when
except in the context of using the form.
What happens in this code if the related row doesn't exist for some reason?
What happens if the related row "The FROM" row is updated in the form?
What happens if either row is deleted from the table?

Default value in data table design

I'm designing a data table in Access. I have two columns whose values are something like:
col1: CU001-
col2: 03
and i want to join these two string to form another column's default value like: CU001-03
How can i do this?
And one more question is whether using a string as main key tends to be much slower than a numeric?
I’m fairly sure (but open to correction) that you cant have this kind of thing in access using the default value however you can get the same effect by doing something like this.
If you have an unbound form then you can set the value of your 3rd column before you save it like this presto code
With rst
!YourCol1=”foo”
!YourCol2=”bar”
!YourCol3= !YourCol1 & !YourCol2
.Update
End with
Search on “unbound forms” on google to see full examples of unbound forms, also if you are only using JET to store your data then use DAO over ADO as it will be faster
EDIT
I have not read any books specifically on access but I can strongly recommend a few access websites that have helped me. Here they are in no particular order
http://www.mvps.org/access/
http://www.granite.ab.ca/access/
http://www.lebans.com/
http://allenbrowne.com/tips.html
There are many more out there but they are a good place to start