?
Solved

VBA on Click select or add record

Posted on 2009-04-09
11
Medium Priority
?
1,035 Views
Last Modified: 2012-05-06
Hi experts//

Hoping someone maybe able to help with this one..What I would like to do is when a user clicks a calendar the db wiill query for the selected job date from the database and if present display the data in my form else if this date is not present it will create a new record. based on the dates already held in the fields... .. I'm very new to this so please excuse any severe blunders.. thanks
Private Sub Calendar5_Click()
 
Me![JD] = Me!Calendar5.Value
Me![JD].Requery
Me![NBD] = GetBusinessDay([JD], 1, "23456", 1, "Holidays", "Holiday Dates")
Me.Refresh
 
Dim rs As DAO.Recordset
Dim sql As String
sql = "SELECT * FROM Calendar INNER JOIN [Job Spec] ON Calendar.[ID] = [Job Spec].[ID] WHERE (((Calendar.[Job Date])= " & Me![JD].Value & "));"
Set rs = CurrentDb.OpenRecordset("Calendar", dbOpenSnapshot)
 If rs.BOF And rs.EOF Then 'IT MEANS THERE ARE NO RECORDS, WE ARE AT THE BEGINNING OF FILE AND END OF FILE
 Else
   Do Until rs.EOF 'DO UNTIL END OF RECORDSET
'BELOW MIGHT NOT BE THE CONDITION YOU ARE AFTER, EXAMPLE ONLY
If rs![Job Date] = [Forms]![Main]![JD] Then 'NOTE FIELD NAMES ARE REFERENCED BY ![]
DoCmd.RunSQL sql
Else
  rs.AddNew
  rs![Job Date] = [Forms]![Main]![JD]
  rs![NextBusinessDay] = [Forms]![Main]![NBD]
  rs.Update
End If
rs.MoveNext 'THIS MOVES TO THE NEXT RECORD IN THE RECORDSET
Loop
'CLEAN UP MEMORY AT END
If Not rs Is Nothing Then
   rs.Close
   Set rs = Nothing
End If
 
End If
 
End Sub

Open in new window

0
Comment
Question by:dodyryda1
  • 6
  • 5
11 Comments
 
LVL 14

Expert Comment

by:RDWaibel
ID: 24107785
How do you plan on displaying the jab info?  On a sub form?
0
 

Author Comment

by:dodyryda1
ID: 24107837
hi RD

yep. I have a Main Form with a subform on and wish to view the results (copy my db at http://www.access-programmers.co.uk/forums/showthread.php?t=169764 )

all i wish to do is check for a date value and display corresponding record, this is not there then add a new record..

Dim rs As DAO.Recordset
Dim sql As String
sql = "SELECT * FROM Calendar INNER JOIN [Job Spec] ON Calendar.[ID] = [Job Spec].[ID] WHERE (((Calendar.[Job Date])= " & Me![JD] & "));"
Set rs = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
 If rs.EOF Then 'IT MEANS THERE ARE NO RECORDS, WE ARE AT THE BEGINNING OF FILE AND END OF FILE
 rs.AddNew
  rs![Job Date] = [Forms]![Main]![JD]
  rs![NextBusinessDay] = [Forms]![Main]![NBD]
  rs.Update
  Me.Requery
End If

Open in new window

0
 
LVL 14

Expert Comment

by:RDWaibel
ID: 24107973
very good.  I just got you db.  Let me take a look
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:RDWaibel
ID: 24108464
First off, you are using two additional calendar controls (I do not have them)

1) CalAdapter 1.1 {C:\Program files\nokia\adapters\cal3a5.dll}
2)CalendarGFI ActiveX {D:\Program files\GFI\LANGaurd 9.0\calendargfi.ocx}

You build an SQL statement but never attach it to a "sub form".  you have the "table" as a sub form.

I would recommend you create a JobDetail subform and set it's record source to the SQL statement.

As I work with this I am finding many issues.  You have teh right idea but I think we need to rethink the process.

There are Many ways to fix this.  I would start by making the main form's recordset the Calendar table.
0
 

Author Comment

by:dodyryda1
ID: 24135563
hi rd thanks .. will give this a ago today...
0
 

Author Comment

by:dodyryda1
ID: 24137581
Hi RD .. thanks have done your suggestion and moved subform recordset to query results... have amende my code but bombing out now with a no current record error when i hit rs.movenext,, any ideas ?
Private Sub Calendar5_Click()
Me.Dirty = False
Me![JD] = Me!Calendar5.Value
Me![JD].Requery
Me![NBD] = GetBusinessDay([JD], 1, "23456", 1, "Holidays", "Holiday Dates")
Me.Refresh
 
Dim rs As DAO.Recordset
Dim sql As String
sql = "SELECT * FROM Calendar INNER JOIN [Job Spec] ON Calendar.[ID] = [Job Spec].[ID] WHERE (((Calendar.[Job Date])= " & Me![JD] & "));"
Set rs = CurrentDb.OpenRecordset("Calendar", dbOpenDynaset)
rs.MoveFirst
If rs.BOF And rs.EOF Then
  rs.AddNew
  rs![Job Date] = [Forms]![Main]![JD]
  rs![NextBusinessDay] = [Forms]![Main]![NBD]
  rs.Update
  Me.Requery
  Else
     Do While Not rs.EOF
    If rs![Job Date] = [Forms]![Main]![JD] Then 'NOTE FIELD NAMES ARE REFERENCED BY ![]
    Set rs = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
    End If
rs.MoveNext 'THIS MOVES TO THE NEXT RECORD IN THE RECORDSET
Loop
End If
If rs.EOF Then
rs.AddNew
  rs![Job Date] = [Forms]![Main]![JD]
  rs![NextBusinessDay] = [Forms]![Main]![NBD]
  rs.Update
  Me.Requery
End If
 
'CLEAN UP MEMORY AT END
If Not rs Is Nothing Then
   rs.Close
   Set rs = Nothing
End If
End Sub

Open in new window

0
 

Author Comment

by:dodyryda1
ID: 24139843
think this is quite difficult so increasing points
0
 
LVL 14

Expert Comment

by:RDWaibel
ID: 24140475
It is because there needs to basically be a rewrite of the main form.
0
 

Author Comment

by:dodyryda1
ID: 24155335
Hi Rd ..

Have rewritten the Main Form recordset is the calendar table and subform is collect from an sql statement how can I get my form to display the record on my form if the If rs![Job Date] = [Forms]![Main]![JD] Then
display the record on the form..
0
 

Accepted Solution

by:
dodyryda1 earned 0 total points
ID: 24159445
solved!! with code below finally nailed the beast!
Private Sub Calendar5_Click()
Me![JD] = Me!Calendar5.Value
Me![NBD] = GetBusinessDay([JD], 1, "23456", 1, "Holidays", "Holiday Dates")
Me.Refresh
 
Dim rs As DAO.Recordset
Set rs = Me.Recordset.Clone
If rs.BOF And rs.EOF Then
  rs.AddNew
  rs![Job Date] = [Forms]![Main]![JD]
  rs![NextBusinessDay] = [Forms]![Main]![NBD]
  rs.Update
  Me.Requery
  Me.Refresh
Else
  rs.FindFirst "[Job Date] = " & "'" & Me![JD] & "'"
   If rs.NoMatch Then
    rs.AddNew
    rs![Job Date] = Me![JD]
    rs![NextBusinessDay] = Me![NBD]
    rs.Update
    Me.Requery
    rs.MoveLast
    Me.Bookmark = rs.Bookmark
    Me.Refresh
   Else
    Me.Bookmark = rs.Bookmark
    Me.Refresh
   End If
End If
    
'CLEAN UP MEMORY AT END
If Not rs Is Nothing Then
   rs.Close
   Set rs = Nothing
End If
 
ExitProc:
   Exit Sub
ProcError:
   MsgBox "Error: " & Err.Number & ". " & Err.Description
   Resume ExitProc
End Sub

Open in new window

0
 
LVL 14

Expert Comment

by:RDWaibel
ID: 24159897
GREAT JOB!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question