Imaging Code

DW

Registered User.
Local time
Today, 10:56
Joined
May 25, 2002
Messages
58
The following code is causing a problem in my db.

On Error GoTo Errtrap
DoCmd.GoToRecord acDataForm, "frmImage", acNewRec
Forms!frmImage!EstimateNo = Forms!frmdetails!EstimateNo
Forms!frmImage!Supp = Forms!frmdetails!Supp
Me.cmdlgpicture.InitDir = "L:\home"
Me.cmdlgpicture.Filter = " Image (*.jpg)|*.jpg"
Me.cmdlgpicture.ShowOpen
Me.olepicture.SourceDoc = Me.cmdlgpicture.FileName
If Me.cmdlgpicture.FileName <> "" Then
Me.olepicture.SizeMode = acOLESizeStretch
Me.olepicture.Action = acOLECreateLink
End If
Exit Sub
Errtrap:

If their is a record already in the table, it works fine.
Adds a new record as it should.
If their is no record and the code is activated,it adds a new record leaving a blank record in the table along with the newly created one.

I know the cause is the first line "acnew etc" but don't know how to bypass this.

Ideally, when the form opens and their are no records to start with, a msgbox to tell the user, their are no records, do you want to add would be great.

Can some-one help

Dave
 
Try this instead

On Error GoTo Errtrap

If Me.RecordsetClone.RecordCount <1 Then

If MsgBox ("There are currently no records available." & vbcrlf & vbcrlf & "Do you wish to add a new record?", vbYesNo, "No Records Available") = vbYes Then
Goto NewRecord
Else: DoCmd.Close '2nd If
End If
Exit Sub

Else: DoCmd.GoToRecord acDataForm, "frmImage", acNewRec '1st If

NewRecord:
Forms!frmImage!EstimateNo = Forms!frmdetails!EstimateNo
Forms!frmImage!Supp = Forms!frmdetails!Supp
Me.cmdlgpicture.InitDir = "L:\home"
Me.cmdlgpicture.Filter = " Image (*.jpg)|*.jpg"
Me.cmdlgpicture.ShowOpen
Me.olepicture.SourceDoc = Me.cmdlgpicture.FileName
If Me.cmdlgpicture.FileName <> "" Then
Me.olepicture.SizeMode = acOLESizeStretch
Me.olepicture.Action = acOLECreateLink
End If

Exit Sub

Errtrap:

Haven't tried it but HTH

[This message has been edited by Fizzio (edited 05-28-2002).]
 
Hi Fizz

I'm still having a problem with this.
I think it is the first line of code.

If Me.RecordsetClone.RecordCount <1 Then

When the form opens, a record is created from the previous mainform, by this the recordcount cannot be <1
Here's the code that opens the form :

Private Sub Command14_Click()
Dim db As DAO.Database
Dim RST As DAO.Recordset
Dim strFilter As String
Dim strsql As String
Dim strValue As String
Set db = CurrentDb
strFilter = "tblImage.EstimateNo = " & Forms!frmdetails!EstimateNo & " And tblImage.Supp = " & Forms!frmdetails.Supp
strsql = "Select * From tblImage Where " & strFilter
Set RST = db.OpenRecordset(strsql, dbOpenDynaset)
If RST.RecordCount = 0 Then
DoCmd.OpenForm "frmImage"
DoCmd.GoToRecord acDataForm, "frmImage", acNewRec
Forms!frmImage!EstimateNo.SetFocus
Forms!frmImage!EstimateNo = Forms!frmdetails!EstimateNo
Forms!frmImage!Supp = Forms!frmdetails!Supp
Else
DoCmd.OpenForm "frmImage", acViewNormal, , strFilter
End If

End Sub

Does this help any ?

Dave
 
Yep that makes a little more sense. You seem to be moving to a new record twice look at your 2 postings. This maybe why you are getting a blank record. Move to a new record only once. Instead of

DoCmd.OpenForm "frmImage"
DoCmd.GoToRecord acDataForm, "frmImage", acNewRec

use DoCmd.OpenForm "frmImage",,,,acFormAdd

which automatically opens the form at a New record (Unless you want to be able to browse the others - use your original code)

You can recycle the if...then code I gave you in the Main form code. But get back if you are stuck.
 
Hi Fizz.

Here's how I did it:
'This opens the form.
Private Sub Command14_Click()
DoCmd.OpenForm "frmImage"
End Sub

And this does the work:

Private Sub cmdPicture_Click()
If Me.RecordsetClone.RecordCount = 0 Then
If MsgBox("There are currently no records available." & vbCrLf & vbCrLf & "Do you wish to add a new record?", vbYesNo, "No Records Available") = vbYes Then
GoTo NewRecord
Else: DoCmd.Close '2nd If
End If
Exit Sub

Else: DoCmd.GoToRecord acDataForm, "frmImage", acNewRec '1st If

NewRecord:
Forms!frmImage!EstimateNo = Forms!frmdetails!EstimateNo
Forms!frmImage!Supp = Forms!frmdetails!Supp
Me.cmdlgpicture.InitDir = "L:\home"
Me.cmdlgpicture.Filter = " Image (*.jpg)|*.jpg"
Me.cmdlgpicture.ShowOpen
Me.olepicture.SourceDoc = Me.cmdlgpicture.FileName
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
If Me.cmdlgpicture.FileName <> "" Then
Me.olepicture.SizeMode = acOLESizeStretch
Me.olepicture.Action = acOLECreateLink

End If
End If
Exit Sub
End Sub

Thanks
DW
 

Users who are viewing this thread

Back
Top Bottom