I have developed a split form which uses the following record source:
SELECT * FROM [CCG Complaints];
The form works well and I have no problems with that side of things.
Users have two buttons: one to search, and one to show all records.
The problems arise when, after performing a search, I close the form and then reopen it again (in form view):
However, whenever I click Show all before closing the form, it opens successfully in form view with no errors.
Another feature is that, after receiving the error mentioned above, I am able to open the form in design view, and can then view it in form view without the error message - the error only occurs when the form is opened in form view in the first instance.
Why is this and what can I do to rectify it?
The code I use for the buttons is here:
Private Sub search_Click()
Dim strSearch As String
Dim strText As String
If Len(Me.txtSearch.Value & vbNullString) = 0 Then
MsgBox ("Enter a search term.")
Else
strText = Me.txtSearch.Value
strSearch = "SELECT* from [CCG Complaints] where [Ref no] Like ""*" & strText & "*"" or [lastName] Like ""*" & strText & "*"" Or CCG Like ""*" & strText & "*"" "
Me.RecordSource = strSearch
End If
End Sub
Private Sub showAll_Click()
Dim strSearch As String
strSearch = "SELECT * from [CCG Complaints]"
Me.RecordSource = strSearch
End Sub
Instead of changing the recordsource, apply a filter to the existing records.
Me.filter = "[Ref no] Like ""*" & strText & "*"" or [lastName] Like ""*" & strText & "*"" Or CCG Like ""*" & strText & "*"" "
Me.FilterOn = True
Then Reset
Me.Filter = ""
Me.FilterOn = False
Be aware that if your record set gets even slightly large, this wildcard searching on lots of fields will become very inefficient. You might want to look at a more robust search method. This http://allenbrowne.com/ser-62.html is a more complete solution.
I suggest to set the RecordSource to [CCG Complaints], and not change it.
Instead use the .Filter properties, this is the usual way to implement a form filter:
Private Sub search_Click()
Dim strSearch As String
Dim strText As String
If Len(Me.txtSearch.Value & vbNullString) = 0 Then
MsgBox ("Enter a search term.")
Else
strText = Me.txtSearch.Value
strSearch = "[Ref no] Like ""*" & strText & "*"" or [lastName] Like ""*" & strText & "*"" Or CCG Like ""*" & strText & "*"" "
Me.Filter = strSearch
Me.FilterOn = True
End If
End Sub
Private Sub showAll_Click()
Me.Filter = ""
Me.FilterOn = False
End Sub
Related
Taken dis codes from random sites using for extract the text content in Slide and Notes section from PPT slides. But the output file given as a NOTEPAD. I want the o/p file as a word document. Can anyone to help on this? Thanks to you in advance
P.S. I express my gratitude those who created these codes and simplify my work.
Option Explicit
Sub ExportNotesText()
Dim oSlides As Slides
Dim oSl As Slide
Dim oSh As Shape
Dim strNotesText As String
Dim strFileName As String
Dim intFileNum As Integer
Dim lngReturn As Long
' Get a filename to store the collected text
strFileName = InputBox("Enter the full path and name of file to extract notes text to", "Output file?")
' did user cancel?
If strFileName = "" Then
Exit Sub
End If
' is the path valid? crude but effective test: try to create the file.
intFileNum = FreeFile()
On Error Resume Next
Open strFileName For Output As intFileNum
If Err.Number <> 0 Then ' we have a problem
MsgBox "Couldn't create the file: " & strFileName & vbCrLf _ & "Please try again."
Exit Sub
End If
Close #intFileNum ' temporarily
' Get the notes text
Set oSlides = ActivePresentation.Slides
For Each oSl In oSlides
strNotesText = strNotesText & "======================================" & vbCrLf
strNotesText = strNotesText & "Slide" & oSl.SlideIndex & vbCrLf
strNotesText = strNotesText & SlideText(oSl) & vbCrLf
strNotesText = strNotesText & NotesText(oSl) & vbCrLf
Next oSl
' now write the text to file
Open strFileName For Output As intFileNum
Print #intFileNum, strNotesText
Close #intFileNum
' show what we've done
lngReturn = Shell("NOTEPAD.EXE " & strFileName, vbNormalFocus)
End Sub
Function SlideText(oSl As Slide) As String
Dim oSh As Shape
Dim osld As Slide
Dim strNotesText As String
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
SlideText = SlideText & oSh.Name & ":" & " " & oSh.TextFrame.TextRange & vbCrLf
End If
End If
Next oSh
End Function
Function NotesText(oSl As Slide) As String
Dim oSh As Shape
For Each oSh In oSl.NotesPage.Shapes
If oSh.PlaceholderFormat.Type = ppPlaceholderBody Then
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
NotesText = oSh.TextFrame.TextRange.Text
End If
End If
End If
Next oSh
End Function
For Example:
Sub Demo()
'Note: A VBA Reference to Word is required.
'See under Tools|References
Dim WdApp As New Word.Application, wdDoc As Word.Document
Dim Sld As Slide, Shp As Shape
Set wdDoc = WdApp.Documents.Add
For Each Sld In ActivePresentation.Slides
With Sld
For Each Shp In .NotesPage.Shapes
With Shp
If .PlaceholderFormat.Type = ppPlaceholderBody Then
If .HasTextFrame Then
If .TextFrame.HasText Then
wdDoc.Range.InsertAfter vbCr & Sld.SlideIndex & ": " & .TextFrame.TextRange.Text
End If
End If
End If
End With
Next
For Each Shp In .Shapes
With Shp
If .HasTextFrame Then
If .TextFrame.HasText Then
wdDoc.Range.InsertAfter vbCr & .Name & ": " & .TextFrame.TextRange.Text
End If
End If
End With
Next
End With
Next
WdApp.Visible = True: wdDoc.Activate
Set wdDoc = Nothing: Set WdApp = Nothing
End Sub
I have this code from another website but it's not working.
I want to filter the split view form from another form.
Private Sub btnSearch_Click()
'//Check that other form is loaded - if not, then open it
If Not fIsLoaded("frmMain") Then
DoCmd.OpenForm ("frmMain")
End If
'//Set filter to listbox criterion
Forms("frmMain").Filter = "[Priorities] = " & Chr(34) & Me.Priorities & Chr(34)
Forms("frmMain").FilterOn = True
End Sub
Function fIsLoaded(ByVal strFormname As String) As Boolean
'Returns False if form is not open or True if Open
If SysCmd(acSysCmdGetObjectState, acForm, strFormname) <> 0 Then
If Forms(strFormname).CurrentView <> 0 Then
fIsLoaded = True
End If
End If
End Function
I received this error
My best guess is the form wasn't ready to take a filter yet, and that's why the error occurred. DoCmd.OpenForm takes a Where argument to set the filter when opening to prevent this from occuring.
Private Sub btnSearch_Click()
'//Check that other form is loaded - if not, then open it with the filter
If Not fIsLoaded("frmMain") Then
DoCmd.OpenForm ("frmMain",,,"[Priorities] = " & Chr(34) & Me.Priorities & Chr(34))
Else
'//Set filter to listbox criterion
Forms("frmMain").Filter = "[Priorities] = " & Chr(34) & Me.Priorities & Chr(34)
Forms("frmMain").FilterOn = True
End If
End Sub
Check if this works. If not, your filter might not be valid for that form.
I'm trying to export one of my queries to email using VBA in a table format. Similar to when you go to external data and click and E-Mail and it adds an attachment to outlook. Except I want it in the body. I put the following code in a button.
I found and made some changes to some code. This is what I have.
Private Sub Command5_Click()
Dim olApp As Object
Dim olItem As Variant
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strQry As String
Dim aHead(1 To 4) As String
Dim aRow(1 To 4) As String
Dim aBody() As String
Dim lCnt As Long
'Create the header row
aHead(1) = "Part"
aHead(2) = "Description"
aHead(3) = "Qty"
aHead(4) = "Price"
lCnt = 1
ReDim aBody(1 To lCnt)
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th> <th>") & "</th></tr>"
'Create each body row
strQry = "SELECT * From qry_email"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)
If Not (rec.BOF And rec.EOF) Then
Do While Not rec.EOF
lCnt = lCnt + 1
ReDim Preserve aBody(1 To lCnt)
aRow(1) = rec("Part")
aRow(2) = rec("Description")
aRow(3) = rec("Qty")
aRow(4) = rec("Price")
aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
rec.MoveNext
Loop
End If
aBody(lCnt) = aBody(lCnt) & "</table></body></html>"
'create the email
Set olApp = CreateObject("Outlook.application")
Set olItem = olApp.CreateItem(0)
olItem.Display
olItem.To = "email#email.com"
olItem.Subject = "Test E-mail"
olItem.HTMLBody = Join(aBody, vbNewLine)
olItem.Display
End Sub
When I run the code, I get a "Run-time error '3061' too few parameters. Expected 1."
If i click debug i get this highlighted in yellow. Anybody help would be greatly appreciated!
Edit
I tried a different approach which actually gave me the list in the body of the email. But it does it for the whole table instead of just the one record I want. This is what the SQL looks like of the query.
SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price
FROM tblePMParts
WHERE (((tblePMParts.WOID)=[Forms]![fmremail]![Text1]));
How would I go about adding the WHERE to the code below.
Private Sub Command4_Click()
'On Error GoTo Errorhandler
Dim olApp As Object
Dim olItem As Variant
Dim olatt As String
Dim olMailTem As Variant
Dim strSendTo As String
Dim strMsg As String
Dim strTo As String
Dim strcc As String
Dim rst As DAO.Recordset
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim qry As DAO.QueryDef
Dim fld As Field
Dim varItem As Variant
Dim strtable As String
Dim rec As DAO.Recordset
Dim strQry As String
strQry = "SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price " & _
"FROM tblePMParts; "
strSendTo = "test#email.com"
strTo = ""
strcc = ""
Set olApp = CreateObject("Outlook.application")
Set olItem = olApp.CreateItem(olMailTem)
olItem.Display
olItem.To = strTo
olItem.CC = strcc
olItem.Body = ""
olItem.Subject = "Please Quote the Following!"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)
If Not (rec.BOF And rec.EOF) Then
rec.MoveLast
rec.MoveFirst
intCount = rec.RecordCount
For intLoop = 1 To intCount
olItem.Body = olItem.Body & rec("[Part#]") & " - " & rec("PartDescription") & " - " & rec("Qty") & " - " & rec("Price")
rec.MoveNext
Next intLoop
End If
MsgBox "Completed Export"
Set olApp = Nothing
Set olItem = Nothing
Exit_Command21_Click:
Exit Sub
ErrorHandler:
MsgBox Err.Description, , Err.Number
Resume Exit_Command21_Click
End Sub
I got it working. Here is the code in case anybody needs it.
Private Sub Command5_Click()
Dim olApp As Object
Dim olItem As Variant
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strQry As String
Dim aHead(1 To 3) As String
Dim aRow(1 To 3) As String
Dim aBody() As String
Dim lCnt As Long
'Create the header row
aHead(1) = "Part#"
aHead(2) = "Description"
aHead(3) = "Qty"
lCnt = 1
ReDim aBody(1 To lCnt)
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th><th>") & "</th></tr>"
'Create each body row
strQry = "SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price " & _
"FROM tblePMParts " & _
"WHERE (((tblePMParts.WOID)=" & [Forms]![fmremail]![Text1] & "));"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)
If Not (rec.BOF And rec.EOF) Then
Do While Not rec.EOF
lCnt = lCnt + 1
ReDim Preserve aBody(1 To lCnt)
aRow(1) = rec("[Part#]")
aRow(2) = rec("PartDescription")
aRow(3) = rec("Qty")
aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
rec.MoveNext
Loop
End If
aBody(lCnt) = aBody(lCnt) & "</table></body></html>"
'create the email
Set olApp = CreateObject("Outlook.application")
Set olItem = olApp.CreateItem(0)
olItem.Display
olItem.To = "Email"
olItem.Subject = "Test E-mail"
olItem.HTMLBody = Join(aBody, vbNewLine)
olItem.Display
End Sub
Somewhere in your code, put a line that says
X = [Forms]![fmremail]![Text1]
Put a breakpoint in your code (hopefully you know how to do that?) on that line. When the code breaks, press F8 to step to the next line, and then type ?X in the Immediate Window. Or you can just hover your mouse over the line with the break point. The point is, you need to see what your code thinks [Forms]![fmremail]![Text1] is equal to. If it's null, you have a problem with your reference. In that case, you may need to add ".Value" or ".Text" to the end of it.
Another thing to check is your datatype for WOID. if it's text, you need to surround it with single quotes.
strQry = "SELECT tblePMParts.[Part#], tblePMParts.PartDescription, tblePMParts.Qty, tblePMParts.Price " & _
"FROM tblePMParts " & _
"WHERE (((tblePMParts.WOID)='" & [Forms]![fmremail]![Text1] & "'));"
I have a form myForm with a text box myText.
In the onLoad function, I have written code that is supposed to append all the rows from a recordset, a database's lockfile, into the myText text box.
What actually happens is, only the first field of four fields is appended into the textbox.
Dim cn As New ADODB.Connection
Dim cn2 As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim i, j As Long
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.Open "Data Source=Z:\myDatabase.mdb"
cn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=Z:\myDatabase.mdb"
Set rs = cn.OpenSchema(adSchemaProviderSpecific, , "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
Me.myText.SetFocus
Me.myText.text = ""
Do While Not rs.EOF
Me.myText.text = Me.myText.text & rs.Fields(0) & vbTab & rs.Fields(1) & vbTab & _
rs.Fields(2) & vbTab & rs.Fields(3) & vbCrLf
rs.MoveNext
Loop
I tried
Debug.Print rs.Fields(0) & vbTab & rs.Fields(1) & vbTab & rs.Fields(2) & vbTab & rs.Fields(3)
and all four fields showed in the immediate window.
EDIT:
I took out the vbtab, since it's not supported and the file itself already has spaces, and I changed Me.myText.text in
Me.myText.text = Me.myText.text & rs.Fields(0) & rs.Fields(1) & _
rs.Fields(2) & rs.Fields(3) & vbCrLf
to Me.myText.
Now my code seems to work in the beginning. When I debug step by step the text box fills like it's supposed to, but when Code exits the do while loop, only the first field of the first row shows in the text box.
debug.print Me.myText before exiting the loop shows all the rows. After exiting the loop still shows all the rows. But in the form, the textbox is only showing the first record, first field.
The form and the textbox both do not have any events other than this event, which is on the forms load.
What is wrong?
Use the property .Value (which is the default property for textbox) , not .Text
so Me.myText.Text ==> Me.myText.Value or Me.myText
The textbox support multi line with vbCrLf.
tab with vbTab is not supported
define myvbTab= " " as a tab separator instead of vbTab
Modify the following fragment of your code:
myvbTab= " "
dim row_data
Do While Not rs.EOF
row_data = rs.Fields(0) & myvbTab& rs.Fields(1) & _
myvbTab & rs.Fields(2) & myvbTab& rs.Fields(3) & vbCrLf
Me.myText= Me.myText & row_data
' be sure that data is displayed, to know if control characters in row_data
Debug.Print row_data
rs.MoveNext
Loop
Edit:
To isolate the problem can you run that code:(independent of reading recordset)
Me.myText.Value = "start "
Dim tabChar As String
tabChar = Chr(9) '" "
Me.myText = Me.myText & "one" & tabChar & "two" & vbCrLf
Me.myText = Me.myText & "three" & tabChar & "four" & vbCrLf
it should display
start one two
three four
I have a word template ready with mail merge fields.
Is there a easy way to convert them into fillable form fields?
In the end I want to create a fillable pdf form.
If it's a simple legacy field:
Public Sub ReplaceMergeFields()
On Error GoTo MyErrorHandler
Dim sourceDocument As Document
Set sourceDocument = ActiveDocument
Dim myMergeField As Field
Dim i As Long
For i = sourceDocument.Fields.Count To 1 Step -1
Set myMergeField = sourceDocument.Fields(i)
myMergeField.Select
If myMergeField.Type = wdFieldMergeField Then
Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
End If
DoEvents
Next
Exit Sub
MyErrorHandler:
MsgBox "ReplaceMergeFields" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
End Sub