With error

C

CJ

Hi Groupies

I am trying to use Open Arguments to open a form and
find a matching inventory item. If the item does not exist
I want to go to a new record. I borrowed the code from an
old thread but I can not quite get it to work for me.

I keep getting "Else without If" when I compile the code.


Private Sub Form_Load()

If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
Else <BREAK POINT>
If .NoMatch Then DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
End If

End Sub



Someday I will get a thorough handle on VBA........sigh....
 
D

Dirk Goldgar

CJ said:
Hi Groupies

I am trying to use Open Arguments to open a form and find a matching
inventory item. If the item does not exist
I want to go to a new record. I borrowed the code from an
old thread but I can not quite get it to work for me.

I keep getting "Else without If" when I compile the code.


Private Sub Form_Load()

If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
Else <BREAK POINT>
If .NoMatch Then DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
End If

End Sub


You've got your first Else in an odd place, and an "inline If" where I think
you want a "block If". I think this is what you had in mind:

If Len(Me.OpenArgs) > 0 Then

With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With

Else

' What do you want to happen if there's no OpenArgs?
' Anything?

End If
 
C

CJ

Hi Dirk

Your code worked perfectly, as usual.

If there is no Open Arguments then I want it to go to a new record.
So, I added a line after the last Else


If Len(Me.OpenArgs) > 0 Then

With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
Else
DoCmd.GoToRecord , , acNewRec
End If

Thanks again Dirk!!

--
Thanks for taking the time!

CJ
---------------------------------------------------------
Know thyself, know thy limits....know thy newsgroups!
Dirk Goldgar said:
CJ said:
Hi Groupies

I am trying to use Open Arguments to open a form and find a matching
inventory item. If the item does not exist
I want to go to a new record. I borrowed the code from an
old thread but I can not quite get it to work for me.

I keep getting "Else without If" when I compile the code.


Private Sub Form_Load()

If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
Else <BREAK POINT>
If .NoMatch Then DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
End If

End Sub


You've got your first Else in an odd place, and an "inline If" where I
think you want a "block If". I think this is what you had in mind:

If Len(Me.OpenArgs) > 0 Then

With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With

Else

' What do you want to happen if there's no OpenArgs?
' Anything?

End If


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
C

CJ

.......and then that last line

DoCmd.GoToRecord , , acNewRec

.....decided not to play my little game.

When I double click on one of my items on my form
I go straight to a new record.....

What I wanted is, if you access the form from a different route
you would go straight to a new record.


--
Thanks for taking the time!

CJ
---------------------------------------------------------
Know thyself, know thy limits....know thy newsgroups!
CJ said:
Hi Dirk

Your code worked perfectly, as usual.

If there is no Open Arguments then I want it to go to a new record.
So, I added a line after the last Else


If Len(Me.OpenArgs) > 0 Then

With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
Else
DoCmd.GoToRecord , , acNewRec
End If

Thanks again Dirk!!

--
Thanks for taking the time!

CJ
---------------------------------------------------------
Know thyself, know thy limits....know thy newsgroups!
Dirk Goldgar said:
CJ said:
Hi Groupies

I am trying to use Open Arguments to open a form and find a matching
inventory item. If the item does not exist
I want to go to a new record. I borrowed the code from an
old thread but I can not quite get it to work for me.

I keep getting "Else without If" when I compile the code.


Private Sub Form_Load()

If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
Else <BREAK POINT>
If .NoMatch Then DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
End If

End Sub


You've got your first Else in an odd place, and an "inline If" where I
think you want a "block If". I think this is what you had in mind:

If Len(Me.OpenArgs) > 0 Then

With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With

Else

' What do you want to happen if there's no OpenArgs?
' Anything?

End If


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

CJ said:
......and then that last line

DoCmd.GoToRecord , , acNewRec

....decided not to play my little game.

Huh. I don't see why not. You mean that line executes, but doesn't send
the form to the new record?
When I double click on one of my items on my form
I go straight to a new record.....

What I wanted is, if you access the form from a different route
you would go straight to a new record.

I'm not understanding this. Where does double-clicking come into it? What
are you doing in the double-click event? Are we talking about the same
form, or some other form?
 
C

CJ

Hi Dirk

Sorry about the lack of info.

I double click on a listbox item on frmTruckLoad
that opens frmInventory with it's open arguments.

Here is the code from both forms:

frmTruckLoad:

Private Sub lstTruckInventory_DblClick(Cancel As Integer)
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmInventory"

stLinkCriteria = "[SKU Number]= '" & Me.lstTruckInventory.Column(0) & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria


frmInventory:

Private Sub Form_Load()
If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
Else
If Len(Me.OpenArgs) < 0 Then
DoCmd.GoToRecord , , acNewRec
End If
End If

I need the last ....acNewRec line in case frmInventory
is opened via the menu. Then I want to go straight to
a new record. At the moment, it is going to the first record
in the data source.
 
D

Dirk Goldgar

CJ said:
Hi Dirk

Sorry about the lack of info.

I double click on a listbox item on frmTruckLoad
that opens frmInventory with it's open arguments.

Here is the code from both forms:

frmTruckLoad:

Private Sub lstTruckInventory_DblClick(Cancel As Integer)
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmInventory"

stLinkCriteria = "[SKU Number]= '" & Me.lstTruckInventory.Column(0) & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria


frmInventory:

Private Sub Form_Load()
If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
Else
If Len(Me.OpenArgs) < 0 Then
DoCmd.GoToRecord , , acNewRec
End If
End If

I need the last ....acNewRec line in case frmInventory
is opened via the menu. Then I want to go straight to
a new record. At the moment, it is going to the first record
in the data source.


Wait a minute! There's something wrong here. I'm looking at these lines in
frmTruckLoad:
stLinkCriteria = "[SKU Number]= '" & Me.lstTruckInventory.Column(0) & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria

That does *not* pass the SKU Number in OpenArgs. It passes a
"where-condition" that filters the form it's opening so that it shows only
the record for the SKU Number that is held in lstTruckInventory.Column(0) --
plus the blank "new record" if the form allows additions. So long as that
is what you want, there is no need for any additional code in frmInventory
to accomplish it.

If you want to detect, in frmInventory, whether it was opened in this way or
not, you could do it like this:

'----- start of code -----
Private Sub Form_Open(Cancel As Integer)

If Me.FilterOn = False Then
DoCmd.GoToRecord , , acNewRec
End If

End Sub
'----- end of code -----

I believe that ought to do it, without the code you currently have in
Form_Load.

If that *doesn't* work, you could use OpenArgs the way you have been
attempting to do, but (a) you must modify the code in frmTruckLoad to pass
the SKU Number in OpenArgs instead of applying a where-condition, and (b)
you must modify the code in frmInventory to allow for the apparent fact that
SKU Number is a text field.
 
C

CJ

Hi Dirk

I used

If Me.FilterOn = False Then
DoCmd.GoToRecord , , acNewRec
End If

and got rid of the rest of the Load code.

Very neat and tidy.

Again, many many thanks!!

--
Thanks for taking the time!

CJ
---------------------------------------------------------
Know thyself, know thy limits....know thy newsgroups!
Dirk Goldgar said:
CJ said:
Hi Dirk

Sorry about the lack of info.

I double click on a listbox item on frmTruckLoad
that opens frmInventory with it's open arguments.

Here is the code from both forms:

frmTruckLoad:

Private Sub lstTruckInventory_DblClick(Cancel As Integer)
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmInventory"

stLinkCriteria = "[SKU Number]= '" & Me.lstTruckInventory.Column(0) & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria


frmInventory:

Private Sub Form_Load()
If Len(Me.OpenArgs) > 0 Then
With Me.RecordsetClone
.FindFirst "[Sku Number]=" & Me.OpenArgs
If .NoMatch Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Bookmark = .Bookmark
End If
End With
Else
If Len(Me.OpenArgs) < 0 Then
DoCmd.GoToRecord , , acNewRec
End If
End If

I need the last ....acNewRec line in case frmInventory
is opened via the menu. Then I want to go straight to
a new record. At the moment, it is going to the first record
in the data source.


Wait a minute! There's something wrong here. I'm looking at these lines
in frmTruckLoad:
stLinkCriteria = "[SKU Number]= '" & Me.lstTruckInventory.Column(0) & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria

That does *not* pass the SKU Number in OpenArgs. It passes a
"where-condition" that filters the form it's opening so that it shows only
the record for the SKU Number that is held in
lstTruckInventory.Column(0) -- plus the blank "new record" if the form
allows additions. So long as that is what you want, there is no need for
any additional code in frmInventory to accomplish it.

If you want to detect, in frmInventory, whether it was opened in this way
or not, you could do it like this:

'----- start of code -----
Private Sub Form_Open(Cancel As Integer)

If Me.FilterOn = False Then
DoCmd.GoToRecord , , acNewRec
End If

End Sub
'----- end of code -----

I believe that ought to do it, without the code you currently have in
Form_Load.

If that *doesn't* work, you could use OpenArgs the way you have been
attempting to do, but (a) you must modify the code in frmTruckLoad to pass
the SKU Number in OpenArgs instead of applying a where-condition, and (b)
you must modify the code in frmInventory to allow for the apparent fact
that SKU Number is a text field.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top