Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2007, 12:50 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default returning null value

Hi,

I have a form where some dates are looked up when the form loads. Here is the code:

Private Sub Form_Load()

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim dtDate As Date

'Open the table for DateZone1 and lookup the value
Set db = CurrentDb
strSQL = "SELECT TOP 1 tblBubbles.autonumber, tblBubbles.Datum, tblBubbles.Hoofdgroep, tblBubbles.Subgroep, tblBubbles.Afdeling, tblBubbles.BubbleNr, tblBubbles.[OK?], tblBubbles.Actie, tblBubbles.DoorgegevenDoor,tblBubbles.DoorgegevenA an, tblBubbles.DoorgegevenOp FROM tblBubbles WHERE (((tblBubbles.Afdeling) = 'BA01')And ((tblBubbles.BubbleNr) = '1'))ORDER BY tblBubbles.Datum DESC;"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

dtDate = rst("Datum")
Me.DateZone1 = dtDate

rst.Close
Set rst = Nothing
Set db = Nothing

'Open the table for DateZone2 and lookup the value
Set db = CurrentDb
strSQL = "SELECT TOP 1 tblBubbles.autonumber, tblBubbles.Datum, tblBubbles.Hoofdgroep, tblBubbles.Subgroep, tblBubbles.Afdeling, tblBubbles.BubbleNr, tblBubbles.[OK?], tblBubbles.Actie, tblBubbles.DoorgegevenDoor,tblBubbles.DoorgegevenA an, tblBubbles.DoorgegevenOp FROM tblBubbles WHERE (((tblBubbles.Afdeling) = 'BA01')And ((tblBubbles.BubbleNr) = '2'))ORDER BY tblBubbles.Datum DESC;"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

dtDate = rst("Datum")
Me.DateZone2 = dtDate

rst.Close
Set rst = Nothing
Set db = Nothing

'Open the table for DateZone3 and lookup the value
Set db = CurrentDb
strSQL = "SELECT TOP 1 tblBubbles.autonumber, tblBubbles.Datum, tblBubbles.Hoofdgroep, tblBubbles.Subgroep, tblBubbles.Afdeling, tblBubbles.BubbleNr, tblBubbles.[OK?], tblBubbles.Actie, tblBubbles.DoorgegevenDoor,tblBubbles.DoorgegevenA an, tblBubbles.DoorgegevenOp FROM tblBubbles WHERE (((tblBubbles.Afdeling) = 'BA01')And ((tblBubbles.BubbleNr) = '3'))ORDER BY tblBubbles.Datum DESC;"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

dtDate = rst("Datum")
Me.DateZone3 = dtDate

rst.Close
Set rst = Nothing
Set db = Nothing

'Open the table for DateZone4 and lookup the value
Set db = CurrentDb
strSQL = "SELECT TOP 1 tblBubbles.autonumber, tblBubbles.Datum, tblBubbles.Hoofdgroep, tblBubbles.Subgroep, tblBubbles.Afdeling, tblBubbles.BubbleNr, tblBubbles.[OK?], tblBubbles.Actie, tblBubbles.DoorgegevenDoor,tblBubbles.DoorgegevenA an, tblBubbles.DoorgegevenOp FROM tblBubbles WHERE (((tblBubbles.Afdeling) = 'BA01')And ((tblBubbles.BubbleNr) = '4'))ORDER BY tblBubbles.Datum DESC;"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

dtDate = rst("Datum")
Me.DateZone4 = dtDate

rst.Close
Set rst = Nothing
Set db = Nothing

'Open the table for DateZone5 and lookup the value
Set db = CurrentDb
strSQL = "SELECT TOP 1 tblBubbles.autonumber, tblBubbles.Datum, tblBubbles.Hoofdgroep, tblBubbles.Subgroep, tblBubbles.Afdeling, tblBubbles.BubbleNr, tblBubbles.[OK?], tblBubbles.Actie, tblBubbles.DoorgegevenDoor,tblBubbles.DoorgegevenA an, tblBubbles.DoorgegevenOp FROM tblBubbles WHERE (((tblBubbles.Afdeling) = 'BA01')And ((tblBubbles.BubbleNr) = '5'))ORDER BY tblBubbles.Datum DESC;"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

dtDate = rst("Datum")
Me.DateZone5 = dtDate

rst.Close
Set rst = Nothing
Set db = Nothing

If IsNull(Me.DateZone1) Or Me.DateZone1 = "" Then
    Me.cmdZone1.Enabled = True
ElseIf Me.DateZone1 = Date Then
    Me.cmdZone1.Enabled = True
    Me.cmdReedsIngevuldZone1.Enabled = True
ElseIf IsNull(Me.DateZone2) Or Me.DateZone2 = "" Then
    Me.cmdZone2.Enabled = True
ElseIf Me.DateZone2 = Date Then
    Me.cmdZone2.Enabled = True
    Me.cmdReedsIngevuldZone2.Enabled = True
ElseIf IsNull(Me.DateZone3) Or Me.DateZone3 = "" Then
    Me.cmdZone3.Enabled = True
ElseIf Me.DateZone3 = Date Then
    Me.cmdZone3.Enabled = True
    Me.cmdReedsIngevuldeZone3.Enabled = True
ElseIf IsNull(Me.DateZone4) Or Me.DateZone4 = "" Then
    Me.cmdZone4.Enabled = True
ElseIf Me.DateZone4 = Date Then
    Me.cmdZone4.Enabled = True
    Me.cmdReedsIngevuldeZone4.Enabled = True
ElseIf IsNull(Me.DateZone5) Or Me.DateZone5 = "" Then
    Me.cmdZone5.Enabled = True
ElseIf Me.DateZone5 = Date Then
    Me.cmdZone5.Enabled = True
    Me.cmdReedsIngevuldeZone5.Enabled = True
ElseIf Me.DateZone1 < Me.DateZone2 Then
    Me.cmdZone1.Enabled = True
ElseIf Me.DateZone2 < Me.DateZone3 Then
    Me.cmdZone2.Enabled = True
ElseIf Me.DateZone3 < Me.DateZone4 Then
    Me.cmdZone3.Enabled = True
ElseIf Me.DateZone4 < Me.DateZone5 Then
    Me.cmdZone4.Enabled = True
ElseIf Me.DateZone5 < Me.DateZone1 Then
    Me.cmdZone5.Enabled = True
End If

End Sub

It took me a lot of time, but it works now... except for 1 little thing.

The database will start as a blanc database so no records will be in the database at startup. If I put some data in the database, the dates are looked up correctly and displayed as I want them to.
If the database starts without any data in the table I get the error

Run-time error '3021': No current record.

Is it possible with a little change in this code to obtain that if no values are found I don't get a VBA error message, but the text boxes where the dates need to appear just remain empty?

 
Old March 5th, 2007, 01:09 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

If rst.RecordCount <> 0 Then
 dtDate = rst("Datum")
 Me.DateZone1 = dtDate
Else
 Me.DateZone1 = "No Data"
End If

This is assuming that Me.DateZone1 - 5 are not date formats. If they are, and this does not work, let me know.

mmcdonal
 
Old March 6th, 2007, 12:02 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is actually a date format, but I changed it to

If rst.RecordCount <> 0 Then
 dtDate = rst("Datum")
 Me.DateZone1 = dtDate
Else
 Me.DateZone1 = ""
End If

and this seems to work...

Thanks again for your help

 
Old March 6th, 2007, 01:54 PM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vince

You are doing a lot of work for something quite easy. Try using a query like this, and handling all 5 in a single recordset:

SELECT tblBubbles.BubbleNr, MIN(tblBubbles.Datum) AS Datum FROM tblBubbles WHERE ((tblBubbles.Afdeling) = 'BA01')) GROUP BY tblBubbles.BubbleNr ORDER BY tblBubbles.BubbleNr;

This gives a single recordset of each of the 5 BubbleNrs in order, together with the earliest date for each. This way, you only have one recordset to check for no rows found. It also has the big advantage of being easily adapted to 4, or 6, or any other number in the future.

Use a While Not rst.EOF loop to process the records, and Me.Controls("DateZone" & rst("BubbleNr")) = rst("Datum") to set the dates into the controls.

Good luck!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning Null Values in a query dstein4d Access VBA 2 March 21st, 2008 03:59 PM
Returning a value Neal XSLT 6 October 13th, 2006 11:41 AM
e.Item.FindControl("DropdownId") Returning Null anup_daware .NET Framework 1.x 1 March 27th, 2006 03:53 PM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Returning Objects BSkelding Pro VB.NET 2002/2003 1 January 11th, 2005 10:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.