Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Form On Load and DataControl

Hello experts,

When I load my form, I code the data control to move to the last record and then the first record. To show the record count displayed in the taskbar. My problem is that each time the form loads I get a "no current record to display" message. I have read the help on the datacontrol and it specifically says that the data control is loaded before the form load event. So why am I getting this error?

Try this on the form load event

datControl.recordset.movelast
datControl.recordset.movefirst

Any help would be appreciated

Much Thanks
Fred
0
gosportlad
Asked:
gosportlad
  • 4
  • 4
  • 2
  • +3
1 Solution
 
sdbanksCommented:
One option is to put the code into the form_activate event instead of the form load event.  If you do this the datcontrols will be initialized completely.  If you only want this count refreshed when the form is loaded.  Put some code around the form activate so that the code does not execute each time the form is activated.
0
 
gosportladAuthor Commented:
I get the same result, whether I put the code in the form activate or the form load event.
0
 
deightonCommented:
'I duplicated your problem then fixed it like this

Private Sub Form_Activate()
   
    If Data1.Recordset.EOF Then
   
        MsgBox "No records"
       
    Else
   
        Data1.Recordset.MoveLast
        Data1.Recordset.MoveFirst
   
        MsgBox CStr(Data1.Recordset.RecordCount)
       
    End If
   
End Sub

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
shchukaCommented:
Try doing this:

dim RecCount as long

dataControl.Refresh
RecCount = 0
if dataControl.RecordSet.RecordCount > 0 then
    dataControl.RecordSet.MoveLast
    dataControl.RecordSet.MoveFirst
    RecCount = dataControl.RecordSet.RecordCount
end if

refresh method explicitely tells data control to open the underlying recordset.
Secondly, you'll get this error even with the data control completely initialized, if there are no records in the recordset.

0
 
deightonCommented:
You have an empty recordset, I couldn't get the data control to work in form load either.
0
 
gosportladAuthor Commented:
Thanks for trying but the datacontrol **does** contain records that is why I cannot understand why the message comes up.


0
 
a111a111a111Commented:
Place in the first line:

example:

Private Sub Form_Load()
On Error Resume Next
'your code here
End Sub



0
 
a111a111a111Commented:
Try this:


Private Sub Form_Load()

On Error Resume Next

Data1.Refresh

Data1.Recordset.MoveLast
Data1.Recordset.MoveFirst

MsgBox Data1.Recordset.RecordCount

End Sub


If you need me to email you the complete project email to shayplace@hotmail.com
0
 
gosportladAuthor Commented:
Thanks to shchuka for the comment,

I needed to refresh the datacontrol each time, *before* I moved the recordset. If shchuka will post his comment as an answer I'll award the points. Thanks to all who helped. The complete form load event now looks like this.

Private Sub Form_Load
   
    Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
    Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
    Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 7500)
    Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
   
On Error GoTo Form_Activate_ErrorHandler

'-------------------- Code Body Starts --------------------
   
    dat1.Refresh 'added thanks to shchuka
    dat1.Recordset.MoveLast
    dat1.Recordset.MoveFirst
   
    Do While Not dat1.Recordset.EOF
    If Not IsNull(dat1.Recordset(0)) Then
        cbo1.AddItem dat1.Recordset(0)
    End If
        dat1.Recordset.MoveNext
    Loop
   
    dat2.Refresh 'added thanks to shchuka
    Do While Not dat2.Recordset.EOF
    If Not IsNull(dat2.Recordset(1)) Then
        cbo2.AddItem dat2.Recordset(1)
    End If

    If Not IsNull(dat2.Recordset(2)) Then
        cbo3.AddItem dat2.Recordset(2)
    End If

    If Not IsNull(dat2.Recordset(3)) Then
        cbo3.AddItem dat2.Recordset(3)
    End If

        dat2.Recordset.MoveNext
    Loop
   
    dat1.Recordset.MoveFirst
   
    Call StatusBarUpdate
    Call UpdateRecords
       
'--------------------- Code Body Ends ---------------------
       Exit Sub
 
   
Form_Activate_ErrorHandler:

    Select Case DisplayMessage(M3021_NO_CURRENT_RECORD, 0, MSG_SYS, 3, , , , " Form_Load ")
    Case vbAbort
      End
    Case vbRetry
      Resume
    Case vbIgnore
      Resume Next
   End Select
End Sub
0
 
TheAnswerManCommented:
To speed things along, you should not do the Row Count this way.

Do This instead.
Dim lr_resultset as rdoresultset
dim ls_SQL as string
ls_SQL = "SELECT COUNT(*) FROM TABLKNAME" _
    &  " SELECT * FROM TableName"
set lr_resultset = rdoconnect.OpenResultset(ls_SQL,rdopenstatic)
Label1 = "Count = " & lr_resultset(0) 'label or whatever
lr_resultset.moreresults
set DATA1.Resultset = lr_resultset


and you are off..  you will really notice a big difference when
it is a larger resultset.



0
 
TheAnswerManCommented:
Use the NextRecordset Sub of the Resultset object
0
 
shchukaCommented:
Apparently, I can't report my comments as an answer, since somebody already locked the question :)

-Aleks.
0
 
gosportladAuthor Commented:
Try it now
0
 
shchukaCommented:
Ok, I'm just reposting my comments :)
Thanks :)



      dim RecCount as long

      dataControl.Refresh
      RecCount = 0
      if dataControl.RecordSet.RecordCount > 0 then
          dataControl.RecordSet.MoveLast
          dataControl.RecordSet.MoveFirst
          RecCount = dataControl.RecordSet.RecordCount
      end if

      refresh method explicitely tells data control to open the underlying recordset.
      Secondly, you'll get this error even with the data control completely initialized, if there are
      no records in the recordset.
0
 
shchukaCommented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now