Normal Topic [Solved] @FormNewRecord () command question (Read 1171 times)
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
[Solved] @FormNewRecord () command question
Jun 1st, 2009 at 1:33pm
Print Post Print Post  
I have following code which get upto 15 records values from M1 to M15 Fields and I1 to I15 Fields from external database and create new records in subform as required.

Code
Select All
var vRSHandle as Int
var nn as Int
var n as Int
var vLoop as Int
var vMedicine as String
var vInstruction as String

var vKey as String
var vNextNumber as String


If @Mode ( ) = 0 then

	{


		vRSHandle = @XResultSetSearch("Data\MedicalTest.db", "Index", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!RecNumber=" + RecNumber)
			if(vRSHandle > -1)
			{
				//vNoOfRecords = @FormResultSetTotal ("Scripts")

				For vLoop = 1 to 15




					vMedicine = @xResultSetValue (vRSHandle, "M" + @Str (vLoop))
					vInstruction = @xResultSetValue (vRSHandle, "I" + @Str (vLoop))

						If vMedicine <> "" then
							{


								nn = @FormNewRecord ("Scripts")
								//WriteLN ("vLoop = " + @str (vLoop))
								//WriteLN ("nn = " + @str (nn))

								FormFieldValue ("Scripts", "Medicine", vLoop, vMedicine)
								FormFieldValue ("Scripts", "Instruction", vLoop, vInstruction)
								FormFieldValue ("Scripts", "RecNumber", vLoop, RecNumber)

								vNextNumber = @ToNumber (@GlobalValue ("ScriptNumber")) + 1   //Create Unique Number for Key field
										//WriteLn ("This is Next Number " + vNextNumber)
										GlobalValue ("ScriptNumber", vNextNumber)
										vNextNumber = @text (7 - @Len (vNextNumber), "0") + vNextNumber
										vKey = vNextNumber
								FormFieldValue ("Scripts", "Key", vLoop, vKey)
								FormCommit ("Scripts")

							}
							Else if vMedicine = "" then  //If no more Regular Medicine then ends the loop
							{
								vLoop =15
							}




				Next

				XResultSetClose(vRSHandle)


				//WriteLN (" vRSHandle is Closed")

			}
	}

//Following Code is to remove empty record at the end

n = @FormResultSetTotal ("Scripts")
//WriteLN ("This is Value of : " + @str (n) )

If @FormFieldValue ("Scripts", "Medicine", n) = "" and @FormFieldValue ("Scripts", "Instruction", n) = "" then
	{
		FormDeleteRecord ("Scripts", n)

	}

 



My question is why nn value it picks up always starts with 2, that creates a blank record at the end in the subform. I would have expected nn value to start with 1 in above code.  I have added the code to remove the blank record at the end but I am trying to understand why nn does not start with 1 in the above code?  Am I clear?
« Last Edit: Jun 3rd, 2009 at 2:59pm by Hammer »  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: @FormNewRecord () command question
Reply #1 - Jun 1st, 2009 at 1:43pm
Print Post Print Post  
Is it a Table View subform?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @FormNewRecord () command question
Reply #2 - Jun 1st, 2009 at 3:06pm
Print Post Print Post  
Yes.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: @FormNewRecord () command question
Reply #3 - Jun 1st, 2009 at 3:33pm
Print Post Print Post  
Bharat_Naik wrote on Jun 1st, 2009 at 3:06pm:
Yes.


A Table View subform always has one blank record waiting to be filled in. For the first subrecord, fill in record #1 instead of calling @FormNewRecord.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @FormNewRecord () command question
Reply #4 - Jun 1st, 2009 at 4:35pm
Print Post Print Post  
I guessed,  it was assumed that at least one record exists in the subform (tableview). But that is not the case here.  How should I fill out the first record? By calling FormResultsetCurrentPosition  ( ) and then using formfieldValue ( )? I tried that for all the records but it kept on crashing. I believe, it was because other records, other than that first one, did not exist in the subform.

Since the first empty subrecord is always there, hopefully it would not crash. If this works, this explains my query.  Thanks Erika.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: @FormNewRecord () command question
Reply #5 - Jun 1st, 2009 at 5:32pm
Print Post Print Post  
Yeah, you can't set current position to a record you haven't made yet.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @FormNewRecord () command question
Reply #6 - Jun 1st, 2009 at 6:03pm
Print Post Print Post  
Thanks Erika.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: @FormNewRecord () command question
Reply #7 - Jun 3rd, 2009 at 2:40pm
Print Post Print Post  
Erika, After filling out the first record using FormResultsetPosition ( ) and FormFieldValue and then using the loop with FormNewRecord ( ) for subsequent records solved the problem and made my understanding clear.  Thanks.
  
Back to top
 
IP Logged
 
Amor
Full Member
Members
***
Offline


No personal text

Posts: 366
Location: Germany
Joined: Feb 7th, 2004
Re: [Solved] @FormNewRecord () command question
Reply #8 - Jun 5th, 2009 at 11:37am
Print Post Print Post  
Hello Bharat,
please show us the final outcome. I need this code for my purposes.

Tthank you!
  

Dr. med. Amor Belhareth&&Medizin Labor &&Germany
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: [Solved] @FormNewRecord () command question
Reply #9 - Jun 5th, 2009 at 3:15pm
Print Post Print Post  
Hello Amor,

Please note that the first record is filled up using ResultSetCurrentPosition ( ) and formfieldValue ( ). And after doing that the rest of the records are filled up using FormNewRecord ( ) using Loop.



Code
Select All
var vRSHandle as Int
var nn as Int
var n as Int
var vLoop as Int
var vMedicine as String
var vInstruction as String

var vKey as String
var vNextNumber as String


If @Mode ( ) = 0 then

	{


		vRSHandle = @XResultSetSearch("Data\MedicalTest.db", "Index", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!RecNumber=" + RecNumber)
			if(vRSHandle > -1)
			{
				// In table view the First Record is always there and it empty. Loop can be used only after that is filled out.

				vMedicine = @xResultSetValue (vRSHandle, "M1" )
				vInstruction = @xResultSetValue (vRSHandle, "I1")

				If vMedicine <> "" and @FormFieldValue ("Scripts", "Medicine", 1) = "" then
				    {
					FormResultsetCurrentPosition ("Scripts", 1)
					FormFieldValue ("Scripts", "Medicine", 1, vMedicine)
					FormFieldValue ("Scripts", "Instruction", 1, vInstruction)
					FormFieldValue ("Scripts", "RecNumber", 1, RecNumber)

					vNextNumber = @ToNumber (@GlobalValue ("ScriptNumber")) + 1   //Create Unique Number for Key field
					//WriteLn ("This is Next Number " + vNextNumber)
					GlobalValue ("ScriptNumber", vNextNumber)
					vNextNumber = @text (7 - @Len (vNextNumber), "0") + vNextNumber
					vKey = vNextNumber
					FormFieldValue ("Scripts", "Key", 1, vKey)
					FormCommit ("Scripts")



				    }

				For vLoop = 2 to 15




					vMedicine = @xResultSetValue (vRSHandle, "M" + @Str (vLoop))
					vInstruction = @xResultSetValue (vRSHandle, "I" + @Str (vLoop))

						If vMedicine <> "" and @FormFieldValue ("Scripts", "Medicine", vLoop) = "" then
							{


								nn = @FormNewRecord ("Scripts")
								//WriteLN ("vLoop = " + @str (vLoop))
								//WriteLN ("nn = " + @str (nn))

								FormFieldValue ("Scripts", "Medicine", vLoop, vMedicine)
								FormFieldValue ("Scripts", "Instruction", vLoop, vInstruction)
								FormFieldValue ("Scripts", "RecNumber", vLoop, RecNumber)

								vNextNumber = @ToNumber (@GlobalValue ("ScriptNumber")) + 1   //Create Unique Number for Key field
										//WriteLn ("This is Next Number " + vNextNumber)
										GlobalValue ("ScriptNumber", vNextNumber)
										vNextNumber = @text (7 - @Len (vNextNumber), "0") + vNextNumber
										vKey = vNextNumber
								FormFieldValue ("Scripts", "Key", vLoop, vKey)
								FormCommit ("Scripts")

							}
							Else if vMedicine = "" then  //If no more Regular Medicine then ends the loop
							{
								vLoop =15
							}




				Next

				XResultSetClose(vRSHandle)


				//WriteLN (" vRSHandle is Closed")

			}
	}
 

  
Back to top
 
IP Logged