Private Sub Form_Load ()
MPort=2 'set the serial port to COM2 MSComm1.Com
MSComm1. Settings="9600, n, 8, 1" 'is 9600 bit/s transmission rate, parity bit, 8 bits of data, one stop
MSComm1. RThreshold=1 'receive buffer every received 1 byte characters, trigger the execution of MSComm control OnComm event () function
MSComm1. SThreshold=0 'data sending events do not produce OnComm events ()
MSComm1. InputLen=1 'use the Input () function when receive buffer of the number of characters, 0 means all read
MSComm1. PortOpen=True 'open the serial port
MSComm1. The Output="6" 'send characters to the single chip microcomputer launched a VB 6 said, asked the MCU send the current setting of the alarm limit
Dim DBConn As New ADODB. Connection
Dim my As String
DBConn. CursorLocation=adUseClient
DBConn. Open the Provider="Microsoft. Jet. The OLEDB. 4.0; Data Source="& amp; App. The Path & amp; "\ database. MDB;"
Dim queryStr As String
QueryStr="select * from measuringrecord order by serial desc"
Dim the rs As New ADODB. You
Set the rs=DBConn. Execute (queryStr)
MSFlexGrid1. Cols=rs. Fields. The Count 'set the number of columns
MSFlexGrid1. Rows=1 'set the number of Rows, at present is only one line can be determined
MSFlexGrid1. ColWidth (0)=1600 'set the width of the column 1
For I=1 To rs. Fields. 'Count - 1 set the width of the remaining columns (equidistance)
MSFlexGrid1. ColWidth (I)=(MSFlexGrid1. Width - MSFlexGrid1. ColWidth (0))/(rs) Fields. The Count - 1)
Next I
For I=0 To rs. Fields. 'Count - 1 set a 1 line value
MSFlexGrid1. ColAlignment (I)=3 'text columns center
MSFlexGrid1. TextMatrix (0, I)=rs. Fields (I). The Name 'first line of each column in the value of measurement records in the database column Name
Next I
The Do While Not rs. EOF 'reading records, until the pointer to the end
MSFlexGrid1. Rows=MSFlexGrid1. Rows + 1 'add a line to store records of read
For I=0 To rs. Fields. 'will read the record Count - 1 on the value of the increased line of
MSFlexGrid1. TextMatrix (MSFlexGrid1 Rows 1, I)=rs. Fields (I) the Value here is the debug window yellow statements and answer
Next I
Rs. MoveNext 'pointer down a record
Loop
End Sub
CodePudding user response:
The above is the content of the database
CodePudding user response:
Never used this, looking at should be rs. Fields (I). The Value null values, the results of cell assignment cannot assign a null reason, an error, in the DEBUG window directly? Isnull (rs) Fields (I). The Value) and see if it is true,MSFlexGrid1. TextMatrix (MSFlexGrid1 Rows 1, I)=rs. Fields (I). The Value
String cellValue
cellValue=https://bbs.csdn.net/topics/iif (isnull (rs) Fields (I). The Value), "", rs. The Fields (I) Value)
MSFlexGrid1. TextMatrix (MSFlexGrid1 Rows 1, I)=cellValue
CodePudding user response:
Or revise the MDB, sets the default column value is "", ensure the data table query results must be not null, your code will not need to change,CodePudding user response:
Still didn't understand the problem wellCodePudding user response:
Empty string in the database in the vb won't recognize into an empty string, and as a null, but the vb string handling is not null, so an error, so you need to add a string type data processing in advance, to identify, using isnull function is null, if it is, don't when the string to the assignment.CodePudding user response:
It is ok to add a statement, Unll actually is an empty string:If the rs. Fields (I). Value<>" "Then MSFlexGrid1. TextMatrix (MSFlexGrid1 Rows 1, I)=rs. Fields (I). The Value
In rs. Fields (I). The Value is not empty string output, output an empty Value is an empty string,