Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check existing record? 1

Status
Not open for further replies.

myzani

Programmer
Jan 20, 2010
10
PH
i have this code but the problem is it's still prompting me that "records already exist"... eventhough it is a new record...

WITH thisform

IF !EMPTY(.txtqty.value) .and. !EMPTY(ALLTRIM(.cbounit.value)) .and. !EMPTY(ALLTRIM(.cboarticle.value))
SCAN
IF ALLTRIM(unit) = ALLTRIM(.cbounit.value) AND ALLTRIM(article) = ALLTRIM(.cboarticle.value)
IF MESSAGEBOX("Unit "+ALLTRIM(unit)+" and "+ALLTRIM(article)+" already in the list. Do you want to edit?", 64+4, "Existing record...") = 6
LOCAL oQty as Form
DO FORM qtyForm NAME oQty WITH .txtqty.value, .cbounit.value, .cboarticle.value LINKED noshow
oQty.show(1)
ENDIF
EXIT
ENDIF
thisform.refresh()
TABLEUPDATE()
ENDSCAN
thisform.Release()
ELSE
MESSAGEBOX('Fill up necessary record(s)!')
ENDIF
 
I think you meant new record as a record which is inserted newly but not tableupdated yet.

scan ... endscan and any other xbase commands operate also on buffered data (unlike SQL - if with (Buffering=.T.) not used).

Simple solution is to also check recno(). For 'new' records recno() is negative.
Code:
IF !EMPTY(.txtqty.value) and ;
 !EMPTY(.cbounit.value) and ;
 !EMPTY(.cboarticle.value)
locate for ;
 ALLTRIM(unit) == ALLTRIM(.cbounit.value) AND ;
 ALLTRIM(article) == ALLTRIM(.cboarticle.value) and ;
 recno() > 0 

if !eof()
    IF MESSAGEBOX("Unit "+ALLTRIM(unit)+" and "+ALLTRIM(article)+" already in the list. Do you want to edit?", 64+4, "Existing record...") = 6
    LOCAL oQty as Form 
    DO FORM qtyForm NAME oQty WITH .txtqty.value, .cbounit.value, .cboarticle.value LINKED noshow
    oQty.show(1)
    ENDIF 
endif

TABLEUPDATE(...) && do not omit parameters, especially alias

thisform.refresh()
endif


Cetin Basoz
MS Foxpro MVP, MCP
 
I don't think using recno() in this case is a good idea.

If their is a record in buffer it needs to be treated as a real record as it could be added at the next tableupdate()

Also personally I'd use "not found()" in place of "not eof()". Found() to me is clearer code.

I like to avoid "if not" when possible. In this case is is with a sligh change. (see below)

My suggestion:
Code:
IF EMPTY(.txtqty.VALUE) AND ;
	EMPTY(.cbounit.VALUE) AND ;
	EMPTY(.cboarticle.VALUE)

	MESSAGEBOX('Fill up necessary record(s)!')
ELSE
	SELECT <your TABLE names goes here>
	LOCATE FOR ;
		ALLTRIM(unit) == ALLTRIM(.cbounit.VALUE) AND ;
		ALLTRIM(article) == ALLTRIM(.cboarticle.VALUE) AND ;
		RECNO() > 0

	IF NOT FOUND([<your TABLE names goes here>])
		IF MESSAGEBOX("Unit "+ALLTRIM(unit)+" and "+ALLTRIM(article)+" already in the list. Do you want to edit?", 64+4, "Existing record...") = 6
			LOCAL oQty AS FORM
			DO FORM qtyForm NAME oQty WITH .txtqty.VALUE, .cbounit.VALUE, .cboarticle.VALUE LINKED NOSHOW
			oQty.SHOW(1)
		ENDIF
	ENDIF
	THISFORM.REFRESH()
	TABLEUPDATE()
	THISFORM.RELEASE()
ENDIF
ENDWITH

I do have a question. is their a reason you added "noshow" to the form and then immediately issue the show command? unless I'm missing something you could do the following with no loss of functionality

Code:
IF MESSAGEBOX("Unit "+ALLTRIM(unit)+" and "+ALLTRIM(article)+" already in the list. Do you want to edit?", 64+4, "Existing record...") = 6
	DO FORM qtyForm WITH .txtqty.VALUE, .cbounit.VALUE, .cboarticle.VALUE
ENDIF
 
I don't think using recno() in this case is a good idea.

If their is a record in buffer it needs to be treated as a real record as it could be added at the next tableupdate()

Also personally I'd use "not found()" in place of "not eof()". Found() to me is clearer code.

I like to avoid "if not" when possible. In this case is is with a sligh change. (see below)

I do think using recno() in this case is a good idea. When tableupdate occurs it is an existing (what you call real) record exactly as expected.

Yes, using 'not found()' in place of 'not eof()' is a personal preference and my preference is !eof(). Actually I don't use found() at all.

I don't like to avoid "if not". However if I were to avoid it then I would write it the correct way:

Code:
IF EMPTY(.txtqty.VALUE) OR ;
    EMPTY(.cbounit.VALUE) OR ;
    EMPTY(.cboarticle.VALUE)

is their a reason you added "noshow" to the form and then immediately issue the show command? unless I'm missing something you could do the following with no loss of functionality

Yes, you are missing something. NoShow, Show(1) allows you to use a non-modal form as modal.



Cetin Basoz
MS Foxpro MVP, MCP
 
My point with the recno() check, looking at his original code sample he's seems to be calling a "new" record as one just entered into the form he's on. He did not have a buffer problem. His actual issue I think is the for on the scan was always finding matches. I think that the locate without the recno() check will fix that.

Damn, forgot to switch the AND/OR's when I was talking about NOT's :(


 
Hmm you have some assumption on what he meant looking at that code. I did look at the code too and didn't really understand what he meant. However, the code doesn't look like that your assumption is true either. If your assumption is true then it is a bigger problem, scan, locate and such would move the pointer.

Cetin Basoz
MS Foxpro MVP, MCP
 
In this kind of support format we have to make some assumptions.

I went into this assuming that his controls were not databound. If that is correct their is no code shown to add a new row to a table. only a method of calling an existing row into an edit window. Thus my assumption that he's not databound may be incorrect. (the more I look at it the more it looks like he's databound)

If his controls are databound their are potential issues based on his buffering mode. The solution to his issue would depend on the mode he is in. Your solution of the recno() check would work if he is databound and set to mode 5. If he's set to mode 3 their won't be a change he'll still get the same message.

In short we need to know:
Are the controls databound?
What buffering mode is being used.

This way we can stop assuming ;)
 
hello, thanks for the replies. i've already solved the problem....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top