I have a query that returns for example 5 rows, I want to show thes these rows(fields) in textboxes. but it only shows me one record. I have also set Default view Property of my form into Continuous form. her is my code:
Private Sub List2_DblClick(Cancel As Integer)
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT XValue, YValue,Wert FROM tb_DCM_Daten WHERE (FzgID=" & Forms!frm_fahrzeug!ID & " AND [Name]='" & List2.Value & "')")
If rst.RecordCount <> 0 Then
Do While Not rst.EOF
Text8.SetFocus
Text8.Text = rst.Fields("XValue").Value
Text10.SetFocus
Text10.Text = rst.Fields("YValue").Value
Text11.SetFocus
Text11.Text = rst.Fields("Wert").Value
rst.MoveNext
Loop
End If
End Sub
how can I do that?
As it stands now, your code would loop through the Recordset, put the values from the first record into the textboxes, then overwrite those values with the ones from the second record, and overwrite those with the values from the third record, and so on.
It sounds like you want a subform. Have a look at the Office tutorial here for more information.
Related
I have two forms: transfert Form with its subform and intransfert Form. I am using
DoCmd.OpenForm "intransfert", , , , acFormAdd, acDialog, Me!Text83
(where text83 is =[transfertasubform].[Form]![transfertadetailid] under
Private Sub Command78_Click()
in transfet form and
Private Sub Form_Load()
On Error Resume Next
If Me.NewRecord Then Me!trnrin = Me.OpenArgs
in intransfet form. intransfert form is based in transfertdetailquery. i wont to prevent passing text83 value more then one time
i tried to explain my problem and expect a help to prevent duplicates when used Arge
assuming trnrin is the name of a variable in your record source. assuming you mean that you want to avoid adding two records where trnrin has the same value and the user sent the same open args twice. assuming trnrin is also the name of a control in the detail section of the intransfert form.
'form load only runs when the form is opened so you have to close the form to pass new args
'but you can just move the same code to a button or whatever
Private Sub IntransferForm_Load()
If Len(Me.OpenArgs) > 0 Then
Me.txtBoxintheHeader = Me.OpenArgs 'the load event is the right place to set controls
'most of this code is to check if there is already an instance where trnrin = the OpenArgs in the record source
Dim lookedupArgs As String
lookedupArgs = Nz(lookedupArgs = DLookup("trnrin", "MyTable", "trnrin = " & "'" & Me.OpenArgs & "'"), "ValuethatisneveranArg")
If lookedupArgs = "ValuethatisneveranArg" Then
'Debug.Print "trnrin = '" & Me.OpenArgs & "'" 'note the string delimiters
'Me.trnrin = Me.OpenArgs 'this surprisingly works but will break easily and doesn't handle complex cases
Dim rs As Recordset
Set rs = Me.Recordset 'if recordset is bound to a table then table will also be updated.
'you can also bind to the table directly but you may need to call me.requery to show the changes
rs.AddNew
rs!trnrin = Me.OpenArgs
rs.Update
End If
End If
End Sub
I am experiencing some odd behavior from a form I am creating in an access database. I have a form called frmSeedling with a subform called frmSeedling detail. frmSeedling gets launched by an event procedure from a command button on a separate form called frmTransect. The OpenArgs passes the primary key of frmTransect called Transect_OID to frmSeedling. Transect_OID is the link field between frmSeedling and frmSeedlingDetail. I have a Form_Current event procedure in frmSeedling to count the number of unique entries on frmSeedling, and create a custom unique id called Seedling_OID. The tables that are the record source for these forms are linked ODBC tables. Below is the code:
Private Sub Form_Current()
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
If Not (rs.EOF) Then
rs.MoveLast
End If
Set rs = Nothing
If IsNull(Seedling_OID) Then
Entry_No = Nz(DCount("Seedling_OID", "rd_Seedling", "Transect_OID = '" & Me.Transect_OID & "'"), 0) + 1
Seedling_OID = Transect_OID & "SD" & Entry_No
End If
End Sub
However, when I launch frmSeedling from frmTransect, I get an error saying I am trying to overwrite the primary key. When I look at the form, this is happening because somehow Access is try to create a new record at the beginning of the form instead of the end of the form, thus thinking that the unique Id I have: created has already been used. Here is a screenshot to show what I mean:
What is curious is that with a separate form called frmDWD with subform frmDWDdetail, I have used this exact setup and it worked fine. Here is the code from frmDWDdetail:
Private Sub Form_Current()
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
If Not (rs.EOF) Then
rs.MoveLast
End If
Set rs = Nothing
If IsNull(DWD_OID) Then
DWD_Piece = Nz(DCount("DWD_OID", "rd_DWD", "Transect_OID = '" & Me.Transect_OID & "'"), 0) + 1
DWD_OID = Transect_OID & "W" & DWD_Piece
End If
End Sub
And here is what that looks like:
Notice how in frmDWD, the record being edited is the last record in the form, while in frmSeedling it is trying to edit the first record in the form. I have set all of the data properties exactly the same in both form and subform, and at least to my eye the code looks identical. Any SQL reasons why I am getting this behavior? Any ideas for fixes? Thanks!!
After hours of playing "Which-of-these-things-is-not-like-the-other" I discovered that the issue was in the property sheet of the subform. I switched "Filter on Empty Master" from "Yes" to "No" and that solved my issue.
You are not using the recordset for anything, so try this reduced code:
Private Sub Form_Current()
Dim Entry_No As Long
If IsNull(Me!Seedling_OID.Value) Then
Entry_No = Nz(DCount("*", "rd_Seedling", "Transect_OID = '" & Me!Transect_OID.Value & "'"), 0) + 1
Me!Seedling_OID.Value = Me!Transect_OID.Value & "SD" & Entry_No
End If
End Sub
I'm runing a query (which is returning the correct data) but how can I loop the recordset and add each line in the form with VBA? Because it's currently adding the last line repeatedly.
Private Sub buscarReservasFio()
Dim strSQL As String
strSQL = "SELECT * FROM tblReservasFio WHERE fk_solicitacao = " & "'" & arrParameters(0) & "'"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
If rs.BOF Or rs.EOF Then
'to-do insert
MsgBox ("Insert")
Else
Do Until rs.EOF
DoCmd.GoToRecord , , acNewRec
Me.ds_funcao = rs.Fields("ds_funcao").Value
Me.fk_titulo = rs.Fields("fk_titulo").Value
Me.nr_cor = rs.Fields("nr_cor").Value
Me.nr_peso = rs.Fields("nr_peso").Value
Me.dt_liberacao = rs.Fields("dt_liberacao").Value
Me.dt_baixa = rs.Fields("dt_baixa").Value
rs.MoveNext
Loop
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
Apparently, form is bound to table but your code is populating UNBOUND controls. That's the only way I can replicate your output - which should be expected. The reason multiple rows of same data show is because controls (BOUND or UNBOUND) repeat for each record and since there is only one set of controls, the same data shows in UNBOUND controls for all rows - last record from recordset loop. Step debug and watch data change on form. The GoToRecord action is meaningless because no data is input to BOUND controls. The result is same without that line.
In order to display multiple records from table, bind controls to fields. Then, options to modify nr_peso field with new calculated value when form first opens:
run an UPDATE action SQL
open a recordset of form's RecordsetClone and loop through that recordset and change value of nr_peso field - changes in RecordsetClone will immediately reflect in the form
code physically moves to each record on form and modifies field value
MS Access arbitrary version.
I have a continuous form which displays a part of a recordset, based on a query that can be sorted in many ways. Let’s say the form displays n records of the recordset (where n>1). The first row in the form isn’t necessary displaying the first record of the recordset. One of the records (let’s say the record on row x of n) is selected.
After a Form.Requery, the first record of the recordset will always be on the first row of the form. That first record is also selected.
That’s not good. I want to restore the form after a Form.Requery so the selected record is still on the x:th row in the form, provided that this is always possible. Which records that is displayed above and beneath the x:th row is obviously depended on the recordset’s underlying query.
In my case, this could be achieved if there was a way to calculate how many rows there is between the selected row and the first row in the form, before Form.Requery.
That calculation would (independently of which part of the recordset that is displayed) result in 0 if the first row of the form is selected, 1 if the second row of the form is selected and so on.
How do I do that?
Or is there is another way to achieve the same thing?
I use this function - see explanations in comments.
'---------------------------------------------------------------------
' Requery a continuous form, keeping the selected record and scroll position as it is before
' With ideas by Stephen Lebans - http://www.lebans.com/SelectRow.htm
'---------------------------------------------------------------------
Public Sub FormRequeryPosition(F As Form)
On Error GoTo Err_FormRequeryPosition
Dim OrigSelTop As Long
Dim RowsFromTop As Long
Dim OrigCurrentSectionTop As Long
' Must cache the current props because Requery will reset them
OrigSelTop = F.SelTop
OrigCurrentSectionTop = F.CurrentSectionTop
' Turn off screen redraw
F.Painting = False
' Requery the Form
F.Requery
' Calculate how many rows, if any, the selected row was from the top prior to the Requery
' Check if Header is visible or not
If F.Section(acHeader).Visible = True Then
RowsFromTop = (OrigCurrentSectionTop - F.Section(acHeader).Height) / F.Section(acDetail).Height
Else
RowsFromTop = OrigCurrentSectionTop / F.Section(acDetail).Height
End If
' Setting the SelTop property forces this row to appear at the top of the Form.
' We will subtract the number of rows required, if any, so that the original
' current row remains at the original position prior to the Requery.
' First set the current record to the last record.
' This is required due to the method that the Access GUI manages the ScrollBar.
' Prevent error on empty form
If F.RecordsetClone.RecordCount > 0 Then
' With complex record source or many records, Access may not know .RecordCount yet -> use MoveLast to get it
F.RecordsetClone.MoveLast
F.SelTop = F.RecordsetClone.RecordCount
F.SelTop = OrigSelTop - RowsFromTop
DoEvents
F.Painting = True
' Now setfocus back to the original row prior to the Requery
' In unknown conditions this raises a runtime error 3001 "Invalid argument"
On Error Resume Next
F.RecordsetClone.AbsolutePosition = F.CurrentRecord + RowsFromTop - 1
If Err.Number = 0 Then
F.Bookmark = F.RecordsetClone.Bookmark
End If
End If
Exit_FormRequeryPosition:
F.Painting = True
Exit Sub
Err_FormRequeryPosition:
MsgBox "Error " & Err.Number & ": " & Err.Description & vbCrLf & "Line: " & Erl
Resume Exit_FormRequeryPosition
End Sub
if you have in the form any unique column then you can do something like this:
Dim uniqueId : uniqueId = me.uniqueField_Value
' uniqueId hold the current selected unique field value
Form.Requery
With Me.RecordsetClone
' if uniqueId is numeric use next line
.FindFirst "[uniqueField] = " uniqueId
' if uniqueId is string uncomment next line and comment previous line
' .FindFirst "[uniqueField] = " & """" & uniqueId & """"
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With
I want to move the visible selected row on the currently-visible table to the last row of the table so that I can manually fill in the fields to create a new record. But I can't find VBA to move the Selection (selected row) to the last row in the open table. My code is shown below, but it fails to do what I want, and I can't find any explanations or examples on the net.
If I had the index of the last row in the table (.RecordCount from somewhere?) and some syntax for setting the visible cursor to that row (maybe records(index).Selected = true or something like that?), I could accomplish my goal. Thank you.
Sub TableLastRowMove()
Dim tbl As AccessObject, tblname As String
tblname = Application.CurrentObjectName
' get a handle to the currently open table and print its name
Set tbl = Application.CurrentData.AllTables(tblname)
Debug.Print tbl.name
' I want to move the visible selected record to the end of the table
' I think I need something like
' set selection = ActiveTable.LastRow
' But that is not available in Access
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM " & tbl.name)
If Not rs.EOF Then
rs.MoveLast
' I want to say
' rs.currentRecord.Selected = true
' but that is not available either
End If
rs.Close
Set rs = Nothing
End Sub
You can do this:
strObjName = "myTable"
DoCmd.OpenTable strObjName
' Go to the new record at the bottom
DoCmd.GoToRecord acActiveDataObject, strObjName, acNewRec
' This would go to the last record
' DoCmd.GoToRecord acActiveDataObject, strObjName, acLast