I have a form (frm_main)
and i have a subform on this form (frm_weblogs_subform)
I am trying to search the subform using a text box.
the code i've found and have been playing with to no avail is:
Private Sub find_weblog_button_Click()
Dim D As Database
Dim wlog As DAO.Recordset
Dim Criteria As String
Set D = CurrentDb
Set wlog = D.OpenRecordset("form_frm_weblogs_subform", dbOpenDynaset)
Criteria = "[weblog_number]='" & [weblogSearch] & "'"
wlog.FindFirst Criteria
wlog.Close
End Sub
It doesn't seem to register the form at all it keeps saying it can't be found.
can anyone help point me in the right direction?
It should read:
Dim wlog As DAO.Recordset
Dim Criteria As String
Set wlog = Me!<NameOfTheSubformCONTROL>.RecordsetClone
If wlog.RecordCount > 0 Then
Criteria = "[weblog_number]='" & [weblogSearch] & "'"
wlog.FindFirst Criteria
If wlog.NoMatch = False
' Found.
Else
' Not found.
End If
End If
wlog.Close
Replace <NameOfTheSubformCONTROL> with that, not the name of the subform.
Related
I have the following example code on a few events for a few comboboxes:
Private Sub cbo_oc_tours_all_Enter()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![cbo_oc_tours_all], Null))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
Me.cbo_oc_tours_today.Value = Str(Nz(Me![cbo_oc_tours_all], Null))
End Sub
I want to put this into a single function and call it from each event procedure(?). I've used the following code in the function:
Sub goto_record(goto_rec As ComboBox, update_rec As ComboBox)
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![goto_rec], Null))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
update_rec.Value = Str(Nz(Me![goto_rec], ""))
End Sub
And I'm trying to call it using:
Sub cbo_oc_tours_all_Enter()
Call goto_record(cbo_oc_tours_all, cbo_oc_tours_today)
End Sub
However, I'm getting an error saying Microsoft Access can't find the field 'goto_rec' referred to in your expression. I'm clearly not passing the combo box in the right way but it's been a while since I've used VBA. What's my issue?
Do not pass the combo box as an object, just pass the value in the combo box. Here is an example:
Sub cbo_oc_tours_all_Enter()
Call goto_record(me.cbo_oc_tours_all, me.cbo_oc_tours_today)
End Sub
Assuming those are the names of the combo boxes themselves. Then you other function would be
'Using a data type of variant for example. Replace with the datatype you are using
Sub goto_record(goto_rec As Variant, update_rec As Variant)
Dim rs As Recordset
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID_T_OC] = " & Str(Nz(goto_rec,0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
'You can change this line to this if the function is in the same module as the form with the combobox
me.cbo_oc_tours_today = goto_rec
End Sub
I have a form which comprise of a texbox bound to a field called Year1. I want to create a new record using the new record controls in the bottom of the form and have the value on the previous record carried forward to the new record. I tried the following codes but no luck. Any help is greatly appreciated. Below is my first approach:
Private Sub Form_Current()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblMyTable")
rs.AddNew
rs.Fields("Year1").Value = Year1.Value
rs.Close
End Sub
I have also tried the following approach but no luck:
Private Sub Form_Current()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblMyTable")
rs.Edit
rs!Year1 = Year1.Value
rs.Update
rs.Close
End Sub
On the Year1_AfterUpdate() even, adding:
Me.Year1.DefaultValue = "'" & Me.Year1 & "'"
Works as expected.
I have created a recordset code that is comparing business id's to grab each customer that's been at this business. I want to pass on this data onto a form through a ubounded textbox or whatver works best for Access. When I have it stored into the unbounded text box in the recordset by using this code in my recordset Forms!Form.Badge = StrBusinesses, it is not showing uniquely on my form per each record/business ID. It shows up the same for each business when I scroll through each record.
How do I get the recordset to show on the form uniquely to each record ?
Public Sub OpenRecordset()
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("QOff2")
qdf.Parameters(0).Value = [Forms]![Form]![Text10]
Dim db As Database
Dim rs As Recordset
Dim StrBusinesses As String
Set rs = qdf.OpenRecordset
If rs.EOF And rs.BOF Then
MsgBox ("No businesses exist for this Customer")
Exit Sub
Else
rs.MoveFirst
End If
StrBusinesses = ""
Do While Not rs.EOF
StrBusinesses = StrBusinesses & rs!Fnam & ", "
rs.MoveNext
Loop
rs.Close
StrBusinesses = Left(StrBusinesses, Len(StrBusinesses) - 2)
Forms!Form.Badge = StrBusinesses
Set rs = Nothing
End Sub
My query sql code:
SELECT badgeno, FNAM, filenum
FROM ((INC LEFT JOIN AIO ON INC.NUM = aio.NUM) LEFT JOIN off ON aio.FNUM = off.FNUM) LEFT JOIN all ON aio.AIO_NUM = all.ALGNUM
WHERE (((FILENUM)=[forms]![form].[text10]));
I am displaying it on a form by creating an unbounded textbox on my form and named it badge (Forms!Form.Badge). While when I push the run/green play button it updates all my unbounded textboxes in each form, so when I see the next record it says the same thing on the previous record. Also, I want it to show automatically without pushing the green play button in the module.
you can change the textbox assignment code like this
Me.yourtextboxname.value = StrBusinesses
In Access 2010 I need to be able to click a command button that will run a query that returns a small two field recordset. Then put the second field in that recordset into a string variable.
This string variable is a link to a word document on the network. the second part of the code will then open the word document.
Any help is GREATLY appreciated.
I am getting the Error: "Object variable or With block variable not set"
My Code looks like this:
`Option Compare Database
Private Sub cmdCESpec_Click()
On Error GoTo Err_cmdCESpec_Click
Dim db As Database
Dim rs As DAO.Recordset
Dim s As String
Dim specSheet As String
s = "SELECT p.PartNum, p.CE_SpecSheet FROM tblParts p WHERE p.PartNum = '" & [Forms]![frmSpecSheet]![cboPartNum] & "'" 'Chooses the correct Spec Sheet.
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet") 'Chooses the Spec Sheet Field
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open (specSheet)
End With
Exit_cmdCESpec_Click:
Exit Sub
Err_cmdCESpec_Click:
MsgBox Err.Description
Resume Exit_cmdCESpec_Click
End Sub
After dissecting your code.. this:
"..WHERE (((tblParts.PartNum)=[Forms]![frmSpecSheet]![cboPartNum]));"
embeds the words Forms!etc into your sql-statement, it doesn't insert the combo-box value into the statement.
You need to take the form reference out of the string:
Dim db As Database
Dim rs As Recordset
Dim s As String
Dim specSheet As String
s = "SELECT tblParts.PartNum, tblParts.CE_SpecSheet FROM tblParts WHERE " _
& "tblParts.PartNum=" & [Forms]![frmSpecSheet]![cboPartNum]
'Chooses the Correct Spec Sheet
Set db = CurrentDb
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet")
'Chooses the Spec Sheet Field
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open ("specSheet")
End With
If the combobox's value is text then you'll need to also surround this with apostrophes.
You also don't need to SELECT the field tblParts.PartNum, you can just refer to it in the WHERE clause.
You haven't posted much information but what I gather you're looking for is Recordset
Dim db As Database
Dim rs As Recordset
Dim s As String
Dim myString As String
s = "SELECT * FROM myTable1;" 'Replace with the SQL you need
Set db = CurrentDb
Set rs = db.OpenRecordset(s)
myString = rs.Fields("myFieldName1") 'Replace myFieldName with the appropriate field name
rs.Close
so if i do a SQL statement like so:
sql = "SELECT * FROM tblMain"
set rs = currentdb.openrecordset(sql)
what method can i use to view every "field name" in this collection i have just created. i am getting some very strange error stating that the item is not found in this collection.
i know the field exists in the table, i have triple checked the spelling everywhere when i reference it, and the SQL should be pulling everything, but i want to see it.
is there a debug.print method to see all these fields
thanks
Justin
This is a variation on the other answers, but I believe it's better to use a For/Each loop than a counter:
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Set rs = CurrentDB.OpenRecordset("SELECT * FROM tblMain")
For Each fld In rs.Fields
Debug.Print fld.Name
Next fld
Set fld = Nothing
rs.Close
Set rs = Nothing
You can iterate through the fields collection of the recordset.
Code is OTTOMH
Dim NumFields as Integer
For NumFields = 0 to rs.Fields.Count -1
Debug.Print Rs.Fields(NumFields).Name
Next
Alternately, you can set a breakpoint at set rs = currentdb.openrecordset(sql) and then as soon as the statement executes, right-click on rs, choose add watch and view the whole thing in the Watches window.
Here is a script that will look for a field containing the string you specify in every table in an Access database (except System and Attached Tables) and write it to text files:
Option Compare Database
Option Explicit
Sub main()
Dim db As Database
Dim rs As Recordset
Dim bFinished As Boolean
Dim sFieldName As String
Dim iPosition, z, x As Integer
Dim bRetVal As Boolean
Dim tdTemp As TableDef
Dim iDatabaseNumbers As Integer
Const FIELD_TO_FIND = "FieldName"
Set db = CurrentDb
Open Left(db.Name, Len(db.Name) - 4) & "_" & FIELD_TO_FIND & ".txt" For Output As #1
For x = 0 To db.TableDefs.Count - 1
Set tdTemp = db.TableDefs(x)
bRetVal = IIf(tdTemp.Attributes And dbSystemObject, False, True)
If bRetVal Then
bRetVal = IIf(tdTemp.Attributes And dbAttachedTable, False, True)
End If
If bRetVal Then
Set rs = db.OpenRecordset(db.TableDefs(x).Name)
If rs.RecordCount > 0 Then
For z = 0 To rs.Fields.Count - 1
sFieldName = rs.Fields(z).Name
If InStr(1, sFieldName, FIELD_TO_FIND, vbTextCompare) > 0 Then
Print #1, db.TableDefs(x).Name
Exit For
End If
Next z
End If
End If
Next x
Close #1
MsgBox "Done"
End Sub
You could adjust accordingly to make it do what you need.