vba cycle through labels to change caption? - ms-access

I have a list of lables like lblData1, lblData2, etc.
I would like to change their caption with a cycle like:
For i = 1 To 10
lblData & i.Caption = "Something"
Next
But logically is not working..how can i achieve this?

Nevermind, I found out I can do:
For i = 1 To X
Controls("lblData " & i).Caption = "Something"
Next

Related

Adding words in bold to table using python-docx

I'm new here (and to python) so any feedback on my post is welcome.
I have some code which asks for an input and then adds it to an entry in various tables.
e.g
import docx
doc = docx.Document('ABC.docx')
length = len(doc.tables)
name = input("What is your name?")
x = range(0,length)
for r in x:
doc.tables[r].cell(0, 1).text = name + ": " + doc.tables[r].cell(0, 1).text
doc.save("ABC_.docx")
and this will take text like "I love you" and change it to "Bob: I love you", which is great. However, I'd like the Bob to appear in bold. How do I do that?
Not sure this is the perfect way to do this, but it works. Basically you store the current cell text in a variable then clear the cell. After that, get the first paragraph of the cell and add formatted runs of text to it, as follows:
import docx
name = input("What is your name?")
doc = docx.Document('ABC.docx')
length = len(doc.tables)
x = range(0,length)
for r in x:
celltext = doc.tables[r].cell(0, 1).text
doc.tables[r].cell(0, 1).text = ""
paragraph = doc.tables[r].cell(0, 1).paragraphs[0]
paragraph.add_run(name).bold = True
paragraph.add_run(": " + celltext)
doc.save("ABC_.docx")
Input:
What is your name?NewCoder
Result:

VBA MS Word content controls messed order

I have a table with about 15 content controls. The content controls have different titles.
Now, I copy-paste the table with content controls a couple of times, and later, get different values into every single content control from the database. Since the content controls from different tables share the same name, I thought of looping through number of tables using something like this
seqNo = 1
For Each t in MyTables
ActiveDocument.SelectContentControlsByTitle("title1").Item(seqNo).Range.Text = "some value 1 from DB"
ActiveDocument.SelectContentControlsByTitle("title2").Item(seqNo).Range.Text = "some value 2 from DB"
' and so on
seqNo = seqNo + 1
Next
The problem is when I use this code, my content controls don't get filled in sequentially. I mean, for example, content control with title title1 from table1 isn't filled with its value, instead, content control with title title1 from table4 gets that value. And this mess goes around really bad: values from table 2 can end up in table 4, 9, 10 and so forth.
I think the order of content controls gets messed up somehow when I copy-paste the tables.
And clue how to get it right?
Didn't really find why this happens, but went with giving unique names to the content controls, like title1, title2, and so on, and then looping through all of them to set the needed values.
Oh my god yes... I have stumbled upon the same annoying issue too. My workaround has been after the copy change the title in code then paste and change that one too (see below). Now my issue is that this takes WAY too long to run since I'm filling out many of these templates in my code. I'm currently at a lose as how to speed this process up or a different approach I should been using.
objWord.ActiveDocument.Range(start:=objWord.ActiveDocument.Tables(3).Range.Rows(1).Range.start, End:=objWord.ActiveDocument.Tables(3).Range.Rows(5).Range.End).Copy
objWord.Selection.EndKey Unit:=wdStory
objDoc.SelectContentControlsByTitle("Date").Item(1).Title = "Date1"
objDoc.SelectContentControlsByTitle("StartTime").Item(1).Title = "StartTime1"
objDoc.SelectContentControlsByTitle("EndTime").Item(1).Title = "EndTime1"
objDoc.SelectContentControlsByTitle("Mins").Item(1).Title = "Mins1"
objDoc.SelectContentControlsByTitle("Note").Item(1).Title = "Note1"
objDoc.SelectContentControlsByTitle("Grp").Item(1).Title = "Grp1"
objDoc.SelectContentControlsByTitle("acc1").Item(1).Title = "acc1_1"
objDoc.SelectContentControlsByTitle("acc2").Item(1).Title = "acc2_1"
objDoc.SelectContentControlsByTitle("acc3").Item(1).Title = "acc3_1"
objDoc.SelectContentControlsByTitle("acc4").Item(1).Title = "acc4_1"
objDoc.SelectContentControlsByTitle("acc5").Item(1).Title = "acc5_1"
objDoc.SelectContentControlsByTitle("acc6").Item(1).Title = "acc6_1"
objDoc.SelectContentControlsByTitle("acc7").Item(1).Title = "acc7_1"
objDoc.SelectContentControlsByTitle("acc8").Item(1).Title = "acc8_1"
For j = 2 To UBound(Narray)
objWord.Selection.Paste
objDoc.SelectContentControlsByTitle("Date").Item(1).Title = "Date" & j
objDoc.SelectContentControlsByTitle("StartTime").Item(1).Title = "StartTime" & j
objDoc.SelectContentControlsByTitle("EndTime").Item(1).Title = "EndTime" & j
objDoc.SelectContentControlsByTitle("Mins").Item(1).Title = "Mins" & j
objDoc.SelectContentControlsByTitle("Note").Item(1).Title = "Note" & j
objDoc.SelectContentControlsByTitle("Grp").Item(1).Title = "Grp" & j
objDoc.SelectContentControlsByTitle("acc1").Item(1).Title = "acc1_" & j
objDoc.SelectContentControlsByTitle("acc2").Item(1).Title = "acc2_" & j
objDoc.SelectContentControlsByTitle("acc3").Item(1).Title = "acc3_" & j
objDoc.SelectContentControlsByTitle("acc4").Item(1).Title = "acc4_" & j
objDoc.SelectContentControlsByTitle("acc5").Item(1).Title = "acc5_" & j
objDoc.SelectContentControlsByTitle("acc6").Item(1).Title = "acc6_" & j
objDoc.SelectContentControlsByTitle("acc7").Item(1).Title = "acc7_" & j
objDoc.SelectContentControlsByTitle("acc8").Item(1).Title = "acc8_" & j
Next

Octave vectorize strsplit return value into separate variables

I have a file with a list of records which I parse one line at a time. Each record is newline delimited and each value is space delimited. This is just a simplified example, but it has a similar structure to the real data.
Bob blue pizza
Sally red sushi
The first value is a name, then their favorite color, then their favorite food. Let's say this is in a processing loop and I want to set variables up for each value. For the first line, my values should look like this.
friendsName = "Bob";
favoriteColor = "blue";
favoriteFood = "pizza";
I read in the line and start out with
lineInFile = "Bob blue pizza";
strsplit seems like a good idea, but it outputs a cell array instead of a matrix of strings and I end up with
strsplit(lineInFile, " ") =
{
[1,1] = Bob
[1,2] = blue
[1,3] = pizza
}
I want something like
{friendsName,favoriteColor,favoriteFood} = strsplit(lineInFile, " ");
This gives me error: invalid lvalue function called in expression
Arrays can be used as lvalues, so I tried
cell2mat(strsplit(lineInFile, " "))
ans = Bobbluepizza
That's not what I want.
This worked.
[friendsName favoriteColor favoriteFood] = strsplit(lineInFile, " "){1,:}

Filter for Text with spaces

Using Microsoft Access I want to set a filter for records which include spaces. I tried double escaping by using '""' to no avail.
I have a table like so:
ID Title
1 Green
2 Blue Yacht
3 Yellow
and a form just displaying these records. When I now set the filter:
Form.Filter = "TestTable.Title LIKE '*Yellow*'"
it works like a charm. But when trying to filter for "Blue Yacht"
Form.Filter = "TestTable.Title LIKE '*Blue Yacht*'"
I get an empty result. Filtering for just Blue works like it is supposed to. Somehow Access doesn't like the spaces in the filter. How can I filter for e.g. "Blue " or "Blue Yacht"?
That's very strange behaviour, it should work fine as is, you could try using the Chr code instead of the space:
Form.Filter = "TestTable.Title LIKE '*Blue" & Chr(32) & "Yacht*'"
I stumbled upon this old thread while searching a solution for the same problem. I found none so far. I wonder if this is a bug on Access or what.
So, this is my case, I tried both filters below. I was working to filter and populate a Datasheet subform. Filters are in Combo Box: Citrate, Paxgene, Sodium Herapin.
Dim sTType as string
...
...
1. sTType = "[Tube Type] LIKE '" & Me.txtTubeType & "*'"
2. sTType = "[Tube Type] ='" & Me.txtTubeType & "'"
...
me.Filter = sTType
When Sodium Herapin is selected and applied as filter , the filter comes up with nothing, while I've no problem with the other word filters.
Sol.: I inserted this code way up
me.txtTubeType = iif(InStr(Trim(Me.txtTubeType), "Sod") > 0, "Sodium*", me.txtTubeType)
...
...
sTType = "[Tube Type] LIKE '" & Me.txtTubeType & "'"
me.Filter = sTType
The work around is kind of crude but it worked for my situation.
Cheers!

how to delete the second occurrence of string in paragraph

I have a Paragraph like this
I saw Susie sitting in a shoe shine shop. Where she sits she shines,
and where she shines she sits.
So that i want to delete the string which occur more than one time. In the above Paragraph she, sits, where, shines repeats more than one times. I want to delete the strings need output like this.
I saw Susie sitting in a shoe shine shop. Where she sits shines, and.
Did anyone guide me to resolve this?
Without allowing for punctuation, you could try something in these lines:
s1 = "I saw Susie sitting in a shoe shine shop. " _
& "Where she sits she shines, and where she shines she sits."
a1 = Split(s1, " ")
For i = 0 To UBound(a1)
For j = i + 1 To UBound(a1)
If a1(j) = a1(i) Then
a1(j) = ""
End If
Next
Next
s2 = Trim(Join(a1, " "))