Limiting RowSource in ComboBox (Access) - forms

I have a form which has a ComboBox on it that pulls all DISTINCT colleague names from a huge table that includes all of our sales (50k+ records). It works perfectly fine, but it takes 3-4 minutes to open the form because it takes so long for Access to find all unique colleague names in the table.
I've been trying to research this and found something that looks useful, but can't seem to get it right.
The code I have at the moment:
Private Sub CollName_Change()
Dim strText As String
strText = Nz(Me.CollName.Text, "")
If Len(strText) > 2 Then
Me.CollName.RowSource = "SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE ""*"" & strText & ""*""; "
Me.CollName.Dropdown
End If
End Sub
I found this code on two forums, this is supposed to do the following: "the key is to not have a Row Source defined for the Combo Box. The row source will be defined as the user starts typing letters. Once they get to 3 letters then the row source of the combo box will be defined and the combo box will be told to dropdown."
When I get to 3 letters, a dropdown appears, but it's blank, it doesn't display any results.
I'm relatively new to Access, although already built two databases, but they all have relatively basic SQL queries, so I have no idea what I'm not doing right here.
Any advice? Or alternatively a different solution as to how take my combo box faster and still keep values unique?

You only have some double-quote mixup there. It is much easier to use single quotes instead of double double-quotes.
Me.CollName.RowSource = _
"SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '*" & strText & "*';"
But your query would be way faster if you would only use the starting letters, i.e. remove the leading *
Me.CollName.RowSource = _
"SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '" & strText & "*';"
But that depends on your requirements.
EDIT to debug:
Dim strText As String
Dim strSelect As String
strText = Nz(Me.CollName.Text, "")
If Len(strText) > 2 Then
strSelect = "SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '*" & strText & "*';"
Debug.Print strSelect
Me.CollName.RowSource = strSelect
Me.CollName.Dropdown
End If
Copy strSelect from the Immediate Window into a query, and try it out. It should help resolve the problem.
See How to debug dynamic SQL in VBA

Related

MS Access - Report to PDF - Data fields not populating in controls on PDF

I am trying to generate a pdf file, based on the contents of an Access Form or a Report (either is fine for me but understand it should be done via a report).
I can generate/populate the form itself, and the report itself, but when I create the pdf with this code, the fields in the pdf are blank. The controls are all there, like seen in my form, but the fields that should have data don't get copied over.
This works as-is for me, and makes a pdf that looks just like my report, except the data from the report does not carry over to the saved pdf
Private Sub pdfBtn_Click()
Dim fileName As String, fldrPath As String, filePath As String
Dim answer As Integer
fileName = "File_Test1" 'filename for PDF file*
fldrPath = "C:\Users\myname\Desktop" 'folder path where pdf file will be saved *
filePath = fldrPath & "\" & fileName & ".pdf"
'check if file already exists
If FileExist(filePath) Then
answer = MsgBox(prompt:="PDF file already exists: " & vbNewLine & filePath & vbNewLine & vbNewLine & _
"Would you like to replace existing file?", buttons:=vbYesNo, Title:="Existing PDF File")
If answer = vbNo Then Exit Sub
End If
On Error GoTo invalidFolderPath
DoCmd.OutputTo acOutputReport, "Report2", acFormatPDF, filePath
MsgBox prompt:="PDF File exported to: " & vbNewLine & filePath, buttons:=vbInformation, Title:="Report Exported as PDF"
Exit Sub
invalidFolderPath:
MsgBox prompt:="Error: Invalid folder path. Please update code.", buttons:=vbCritical
End Sub
Function FileExist(FileFullPath As String) As Boolean
Dim value As Boolean
value = False
If Dir(FileFullPath) <> "" Then
value = True
End If
FileExist = value
End Function
I've spent a few hours searching, so I'd be upset if it is something overly simple and available. But is anyone able to point me in the right direction with this?
It looks like I had some logic wrong. Yes the code provided does work, thanks again #June7. I was over complicating things by trying to generate the pdf off the form itself, versus off a report. I ended up making the button pop up the report instead of another form, and the data does now flow as expected.
Silly, like I said :(

Update multiple source links for pictures in WORD 2016 via VBA

I worked on some VBA code to change all the sourcelocation of all the images. However it is detecting only 3 InlineShapes. There are many more. I inserted all images as Insert and LInk
Sub ChangesSource()
Dim i As Long
Documents("Document.docx").Activate
Debug.Print ActiveDocument.Name
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
'.shp.LinkFormat.SourceFullName =
.LinkFormat.SourceFullName = Replace(.LinkFormat.SourceFullName, "C:\oldLink", "C:\newLink")
Debug.Print .LinkFormat.SourceFullName
'Debug.Print InlineShapes(i).SourceFullName
End With
Next i
End With
End Sub
Try:
Sub ChangeSource()
Dim i As Long
With ActiveDocument
For i = .InlineShapes.Count To 1 Step -1
With .InlineShapes(i)
If Not .LinkFormat Is Nothing Then
With .LinkFormat
.SourceFullName = Replace(.SourceFullName, .SourcePath, "C:\NewPath\")
End With
End If
End With
Next i
End With
End Sub
If you save the document in the older .doc format, pressing Alt-F9 will expose the INCLUDEPICTURE fields containing the links. You can then use Find/Replace to update the paths (note the use of \\ for the path separators). Pressing Alt-F9, then Ctrl-Shift-F9 will refresh the linked images. You can then re-save the document in the docx format, if you wish. The other option is to use a macro.

SSRS expression - remove line breaks when field contains no data

I have an SSRS report that is a calendar, and it shows each persons different activity for each day. I currently do this via .vbcrlf to create the line break.
What I would like to do is only return a line break when there is data in the specific value.
My current expression is:
=Fields!DayOfMonth.Value
& Constants.vbcrlf
& Constants.vbcrlf
& Fields!Shift.Value
& Constants.vbcrlf
& Fields!OT.Value
& Constants.vbcrlf
& Fields!Holiday.Value
& Constants.vbcrlf
& Fields!AbsenceType.Value
& Constants.vbcrlf
& Fields!ClockIn.Value
So if there is no Holiday I would like to remove that line and the break meaning Absence and In/Out would move up the text box. I've tried using isNothing but cant get the syntax right.
It sees the data as NULL values, which can alter the way it treats text.
You can use iif(isnothing(Field!Thing.Value), "", Constants.vbcrlf & Field!Thing.Value) around each one to replace null values with blanks.
There may be a more elegant solution available.
The syntax which worked for me in VS 2017 with SQL Server Data Tools:
=IIf(Fields!Holiday.Value Is "", "", Fields!Holiday.Value + Environment.NewLine)
+ IIf(Fields!AbsenceType.Value Is "", "", Fields!AbsenceType.Value + Environment.NewLine)
Code is pretty much self explanatory - if there is no value for Holiday, result is empty string or else if it has value, show value and line break. For latest syntax of any function, just refer to the examples given in the Expression window.

PostgreSQL: Save query results to file and get number of saved rows, windows/odbc

I am trying to save query results to text file automatically, without looping through reader object in VB.NET with using ODBC windows connection.
But I can't find how!
That's what I try so far:
mCmd = New OdbcCommand( _
"SELECT my_id FROM " & myTable & " WHERE myflag='1' \o 'c:/a_result.txt'", mCon)
n = mCmd.ExecuteNonQuery
But that don't work at all.
Please advice or code example how to get it.
And second...
It will be ideally that with saving results to text I get a number of saved rows in variable 'n'.
As for now I get only 0 or 1 depends if query was successful or not.
EDIT:
After some fighting I found a way for do this with more or less success.
To txt file:
mCmd = New OdbcCommand( _
"COPY (SELECT my_id FROM " & myTable & " WHERE myFlag='1' " & _
"ORDER BY my_id) TO 'c:/a_result.txt' DELIMITER AS '|'", mCon)
To csv file:
mCmd = New OdbcCommand( _
"COPY (SELECT my_id FROM " & myTable & " WHERE myFlag='1' " & _
"ORDER BY my_id) TO 'c:/a_result.csv' WITH CSV", mCon)
That works, but I am not able to escape quotes and '\' so I got double signs in output file.
If someone with experience know how to achieve escaping and changing delimiter for csv files I would be glad to see it on given example.
Variable 'n' after query contain a number of exported rows.
The \o sequence is a psql meta-command. That means it is a feature of psql. If you want this functionnality you will have to implement it in your client. It is very easy though.

Tool to merge MS Word files

I have huge number of Word files I need to merge (join) into one file, and will be time consuming to use the Word merger (one by one). Have you experienced any tool that can handle this job?
Sub MergeAllDocuments(AllDocumentsPath as String, MasterDocumentPath as String)
Dim MasterDocument As Document
Set MasterDocument = Documents.Open(FileName:=MasterDocumentPath)
TheDocumentPath = Dir(AllDocumentsPath , vbNormal)
While TheDocumentPath <> ""
' Append the next doc to the end of the master doc. (The
' special "\EndOfDoc" bookmark is always available!)
MasterDocument.Bookmarks("\EndOfDoc").Range.InsertFile TheDocumentPath
TheDocumentPath = Dir
Wend
MasterDocument.Save
End Sub
MergeAllDocuments "C:\MySeparateDocuments\*.doc", "C:\MasterDocument.doc"
I have one question - why do you want do do such a thing (with a "huge number" of documents, at least)?
I came across a post by Graham Skan a while back. It might get you started:
Sub InsertFiles()
Dim strFileName As String
Dim rng As Range
Dim Doc As Document
Const strPath = "C:\Documents and Settings\Graham Skan\My Documents\Allwork\" 'adjust as necessary '"
Set Doc = Documents.Add
strFileName = Dir$(strPath & "\*.doc")
Do
Set rng = Doc.Bookmarks("\EndOfDoc").Range
If rng.End > 0 Then 'section break not necessary before first document.'
rng.InsertBreak wdSectionBreakNextPage
rng.Collapse wdCollapseEnd
End If
rng.InsertFile strPath & "\" & strFileName
strFileName = Dir$()
Loop Until strFileName = ""
End Sub
Have you tried using the Word COM api? You can automate lots of things - maybe you can automate a merge.
Do you really need to do an actual merge, or do you want to join the files together. The two things are quite different.
Merging is used when you have two versions of an original file with (potentially conflicting) changes. I can't really see how you would have a "huge number" of files that you needed to merge all together. This would be an absolute nightmare of conflicts. Do you mean to merge sets of them into individual files?
Joining would be when you want to concatenate them one after the other. This would be a lot easier to do. This is quite possible using the COM api.