Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help with inserting records!!!
Message
From
09/10/2006 18:52:54
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Help with inserting records!!!
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01160723
Message ID:
01160723
Views:
58
I have this problem.
when I click the insert button i want to insert a blank record before the actual one having the field campo1 equal to the actual record.
As you can see, happens that the value of the fourth field replace after I skip to the next record, the fourth field of the following record.

A little explain of what i did :
In the form.load I create a cursor and fill it randomly with 30 records.
The active record is shown in the container under the grid and i want to skip to the next/previous record with left/right Arrow key, Tab/sh+tab, dwn/up arrow key if the control which has the focus is the Last/First control in the container.
To Indiate which is the first or the last control I create for the textbox the properties FirstField/LastField.
If I skip to the next record and I am at Eof() then I append a new record
which should have the field named as the value of container.fieldtoadd equal to the previous record (Hope it's clear!!!)
If I want to insert a record before the active one I use the command Button.

And Here comes the problem : The new record is correctly insert but after i skip to the next one, the value of its last field fills the last field of the next record. Why ?


thanks
Alessio
public oFrm
oFrm = CreateObject('Myform')
With oFrm
	.visible = .t.
	.width = 500
	.height = 300
EndWith 

Return 	

Define Class MyForm as Form 
windowtype = 1
Procedure load 
	Create Cursor 'wprova' (riga c(3),insbyte c(1),campo1 c(10),CAMPO2 C(20), campo3 c(10), campo4 c(10))
	*Index on riga + insbyte Tag 'chiave'
	For i = 1 to 30
		m.riga = Padl(Alltrim(Str(i)),3,'0')
		m.insbyte = '0'
		campo1 = m.riga
		campo2 = Replicate(Chr(Ceiling(Rand()*72)+32),20)
		campo3 = Replicate(Chr(Ceiling(Rand()*72)+32),10)
		campo4 = Replicate(Chr(Ceiling(Rand()*72)+32),10)

		Insert into 'wprova' from MEMVAR 
	EndFor 
	Locate 	
EndProc  

Procedure Init
	this.AddObject('grid1','Mygrid')
	With this.grid1
		.recordsource = ''
		.width = 400
		.columncount = 4
		.height = 150
		.top = 0
		.left = 10
		.recordsource = 'wprova'
		.column1.controlsource = 'wprova.campo1'
		.column2.controlsource = 'wprova.campo2'
		.column3.controlsource = 'wprova.campo3'
		.column4.controlsource = 'wprova.campo4'			
		.visible = .t.
	EndWith 
	this.AddObject('cntdocdet'	, 'cntdocdet')
	With this.cntdocdet
		.left = 10
		.width = 450
		.top = this.grid1.top + this.grid1.height + 10
		.visible = .t.
	EndWith 
	this.cntdocdet.ogrid = this.grid1 
EndProc 

EndDefine 


Define Class pls as CommandButton 

	Procedure click
		Local array arCtl(1,2)
		lnObj = 0
		For each oCtrl in this.Parent.Objects 
			lnobj = lnObj + 1
			If Lower(oCtrl.baseclass) = 'textbox' or ;	 
				Lower(oCtrl.baseclass) = 'combobox' or ;
					Lower(oCtrl.baseclass) = 'editbox' or ;
						Lower(oCtrl.baseclass) = 'checkbox' or ; 
							Lower(oCtrl.baseclass) = 'optiongroup'
				Dimension arCtl(lnObj,2)
				arCtl(lnobj,1) = oCtrl
				arCtl(lnobj,2) = oCtrl.controlsource
				octrl.controlsource = ''
			EndIf 
		EndFor 					
		With this.Parent.ogrid
			lcalias = .recordsource 
			.recordsource = ''
			Select (lcAlias)
			Skip -1 in (lcAlias)
			m.riga	 = '000'
			m.insbyte= '0'
			If !Bof(lcAlias)
				m.riga = riga
				m.insbyte = '1'
			EndIf 
			Insert into (lcAlias) (riga,insbyte)	VALUES (m.riga,m.insbyte)
			Select * from (lcAlias) into cursor 'wprova' order by riga,insbyte READWRITE 
			Locate for riga + insbyte == m.riga + m.insbyte
			lnrecno = Recno()
			Replace riga with Padl(Alltrim(Str(Recno())),3,'0'), insbyte with '0' all
			Go lnrecno 
			.recordsource = lcAlias
			.column1.controlsource = lcalias+'.campo1'
			.column2.controlsource = lcalias+'.campo2'
			.column3.controlsource = lcalias+'.campo3'
			.column4.controlsource = lcalias+'.campo4'			
			*---
			.setfocus()
		EndWith 		

		For lni = 1 to Alen(arCtl,1)
			arCtl(lni,1).controlsource = arCtl(lni,2)
		EndFor 	
		this.Parent.txtgen2.SetFocus()
		this.Parent.Refresh()
	EndProc 
EndDefine 


Define Class 'txtgen' as TextBox 
	firstfield = .f.
	lastfield = .f.

	Procedure keypress
	LPARAMETERS nKeyCode, nShiftAltCtrl
	Do case 
		Case nKeyCode = 23 and nshiftAltCtrl = 2
		*- ctrl + w	
			this.DblClick()
		Case  nkeyCode = 56 and nShiftAltCtrl = 1
		*- shift Arup
			this.Parent.recordmove(-1)

		Case nkeyCode = 50 and nShiftAltCtrl = 1
		*- shift Ardwn
			this.Parent.recordmove(1)

		Case nKeyCode = 3
			this.Parent.recordmove(2)
			* pgDwn
		Case nKeyCode = 18
			this.Parent.recordmove(-2)
			* pgUp
		Case this.Firstfield and ;
			( (nKeyCode = 5 and nShiftAltCtrl = 0) or ;
				(nKeyCode = 19 and nShiftAltCtrl = 0)  or ;
				(nKeyCode = 15 and nShiftAltCtrl = 1)) 
			this.Parent.recordmove(-1)
		Case this.lastfield and ;
			( (nKeyCode = 13 and nShiftAltCtrl = 0 or nShiftAltCtrl = 1) or ;
				(nKeyCode = 24 and nShiftAltCtrl = 0)  or ;
				(nKeyCode = 4 and nShiftAltCtrl = 0)  or ;
				(nKeyCode = 9 and nShiftAltCtrl = 0)) 
			this.Parent.recordmove(1)

	endCase 	
EndDefine 

*-----------------------------------------------------------
Define  class 'cntdocdet' as Container 
	FieldToAdd = ''
	oGrid		 = ''

	Procedure Init 
	this.AddObject('txtgen1','txtgen')
	With this.txtgen1
		.top = 10
		.left = 100
		.width = 100
		.controlsource = 'wprova.campo1'
		.firstfield = .t.
		.visible = .t.
	EndWith 
	
	this.AddObject('txtgen2','txtgen')
	With this.txtgen2
		.top = 10
		.left = 250
		.width = 100
		.controlsource = 'wprova.campo2'
		.visible = .t.
	EndWith 		

	this.AddObject('txtgen3','txtgen')
	With this.txtgen3
		.top = 40
		.left = 10
		.width = 100
		.controlsource = 'wprova.campo3'
		.visible = .t.
	EndWith 		

	this.AddObject('txtgen4','txtgen')
	With this.txtgen4
		.top = 40
		.left = 250
		.width = 100
		.controlsource = 'wprova.campo4'
		.lastfield = .t.
		.visible = .t.
	EndWith 		

	this.AddObject('cmd','pls')
	With this.cmd
		.top = 0
		.left = 0
		.height = 18
		.caption = 'Insert'
		.visible = .t.
	EndWith 		

	Procedure RecordMove 
	Parameters xmove
	Select (this.oGrid.recordsource)
	Do case 
		Case xMove = 1
			* sposto di un record in avanti
			If !Empty(this.fieldtoadd)	
				Scatter MEMVAR fields Like (this.fieldtoadd)
			EndIf 	
			if !eof(this.oGrid.recordsource)
				skip in (this.oGrid.recordsource)
			EndIf 
			if eof(this.oGrid.recordsource)
				Append Blank in (this.oGrid.recordsource)
				m.fieldtoadd = Evaluate('m.'+this.fieldtoadd)
	*			this.ogrid.setfocus()
				this.txtgen1.value = m.fieldtoadd
				this.Refresh()

			endif		
			this.txtgen2.setfocus()				

			
		Case xMove = -1 
			* record precedente
			if not bof(this.oGrid.recordsource)
				skip -1 in (this.oGrid.recordsource)
			endif			
		Case xMove = -2
			*- pagina su
			this.oGrid.DoScroll(2)
			THIS.ogrid.ActivateCell(1,1)
		Case xMove = 2
			* pagina giù				
			this.oGrid.DoScroll(3)
			this.ogrid.ActivateCell(1,1)

	EndCase 

	this.oGrid.setfocus()

EndDefine


Define Class MyGrid as Grid 
Procedure afterrowcolchange
LPARAMETERS nColIndex
With thisform.cntdocdet
	.refresh()
	.txtgen2.SetFocus()
EndWith 	
EndProc
EndDefine 
	
Next
Reply
Map
View

Click here to load this message in the networking platform