| |

VerySource

 Forgot password?
 Register
Search
View: 1803|Reply: 8

Automatically generate codes in the table

[Copy link]

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-4 12:30:01
| Show all posts |Read mode
I want to automatically generate a number field in a table when a new record is added, but not repeat it. How do I write code? Please advise!
Reply

Use magic Report

0

Threads

32

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2021-3-4 12:45:01
| Show all posts
Use self-increment fields above VFP7.0.

If it is below VFP7.0:

select max(field 1)+1 as MaxID into cursor t1 from table name
insert into table name (field 1) values ​​(t1.MaxID)
Reply

Use magic Report

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2021-3-4 13:45:01
| Show all posts
Can you explain the meaning of the code in detail? Thank you
Reply

Use magic Report

0

Threads

32

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2021-3-4 14:00:01
| Show all posts
select max(field 1)+1 as MaxID into cursor t1 from table name

Take the maximum number and store +1 in the MaxID field of the temporary table T1.


insert into table name (field 1) values ​​(t1.MaxID)
select table name
go bott
replace ....&&Here is what you want to write

Before you add records, write the number with the largest number+1 into the table
Reply

Use magic Report

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2021-3-4 14:15:01
| Show all posts
"Select table name
go bott
replace ....&&Here is what you want to write

Before you add records, write the number with the largest number+1 into the table"
I still don't understand this passage. I will explain it in detail. In addition, is the sentence you said directly added to the "click" event of the newly added control button?
There is another question. I want to display the results of the query in a multi-condition query form directly in the form of a table. At the same time, I can print or export the results of this query in the form of a report. How to achieve it? Many thanks!
Reply

Use magic Report

0

Threads

32

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2021-3-4 14:30:01
| Show all posts
Put the following code in the Click event of your add button:

select max(number)+1 as MaxID into cursor t1 from table name&&takes out the maximum number and +1 is stored in the MaxID field of the temporary table T1.
Insert into table name (number) values ​​(t1.MaxID)
select table name
go bott
replace ....&&Here is what you want to write
Reply

Use magic Report

0

Threads

32

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2021-3-4 14:45:01
| Show all posts
There is another question. I want to display the results of the query in a multi-condition query form directly in the form of a table. At the same time, I can print or export the results of this query in the form of a report. How to achieve it? Many thanks!
-------------------------------------

Use GRID control:

Click event of query button:

thisform.grid1.recordsource=null
select ...into cursor t1 from ...&&This sentence is your query
thisform.grid1.recordsource='t1'
thisform.grid1.refresh
Reply

Use magic Report

1

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2021-3-4 15:00:01
| Show all posts
If your number is out of order, you can use a random number, if it is ordered, you can write it in CLICK
use table name
go boot
bb=table name.number

appe black
repl table name. number with bb+1
...
This one is simpler than
If I follow the procedure of Thirteen Beans, my understanding is as follows, please correct me if it is wrong
Write the code in CLICK
select max(field 1)+1 as MaxID into cursor t1 from table name
insert into table name (field 1) values ​​(t1.MaxID)&&insert a new record in the last record of the table name, so that APPE BLABK can be omitted

select table name
go bott&&points to the last record, which is the record that was inserted at the moment
replace ....&&Here is what you want to write
Reply

Use magic Report

1

Threads

19

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 Ukraine

Post time: 2021-3-4 16:45:01
| Show all posts
The easiest way:
    Set the default value of the "No." field in the table designer:
                         If the field is numeric, the default value is RECNO()
                         If the field is a character type, the default value is allt(str(RECNO()))
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list